Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TryGet... methods to USIOJsonObject #440

Merged
merged 1 commit into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Source/SIOJson/Private/SIOJsonObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ void USIOJsonObject::SetField(const FString& FieldName, USIOJsonValue* JsonValue
//////////////////////////////////////////////////////////////////////////
// FJsonObject API Helpers (easy to use with simple Json objects)

bool USIOJsonObject::TryGetNumberField(const FString& FieldName, float& OutNumber) const
{
return JsonObj.IsValid() && JsonObj->TryGetNumberField(FieldName, OutNumber);
}

float USIOJsonObject::GetNumberField(const FString& FieldName) const
{
if (!JsonObj.IsValid() || !JsonObj->HasTypedField<EJson::Number>(FieldName))
Expand All @@ -184,6 +189,12 @@ void USIOJsonObject::SetNumberField(const FString& FieldName, float Number)
JsonObj->SetNumberField(FieldName, Number);
}


bool USIOJsonObject::TryGetStringField(const FString& FieldName, FString& OutString) const
{
return JsonObj.IsValid() && JsonObj->TryGetStringField(FieldName, OutString);
}

FString USIOJsonObject::GetStringField(const FString& FieldName) const
{
if (!JsonObj.IsValid() || !JsonObj->HasTypedField<EJson::String>(FieldName))
Expand All @@ -205,6 +216,11 @@ void USIOJsonObject::SetStringField(const FString& FieldName, const FString& Str
JsonObj->SetStringField(FieldName, StringValue);
}

bool USIOJsonObject::TryGetBoolField(const FString& FieldName, bool& OutBool) const
{
return JsonObj.IsValid() && JsonObj->TryGetBoolField(FieldName, OutBool);
}

bool USIOJsonObject::GetBoolField(const FString& FieldName) const
{
if (!JsonObj.IsValid() || !JsonObj->HasTypedField<EJson::Boolean>(FieldName))
Expand Down Expand Up @@ -317,6 +333,25 @@ void USIOJsonObject::MergeJsonObject(USIOJsonObject* InJsonObject, bool Overwrit
}
}

bool USIOJsonObject::TryGetObjectField(const FString& FieldName, USIOJsonObject*& OutObject) const
{
if (!JsonObj.IsValid())
{
return false;
}

const TSharedPtr<FJsonObject>* JsonObjField;
bool bSuccess = JsonObj->TryGetObjectField(FieldName, JsonObjField);

if (bSuccess)
{
OutObject = NewObject<USIOJsonObject>();
OutObject->SetRootObject(*JsonObjField);
}

return bSuccess;
}

USIOJsonObject* USIOJsonObject::GetObjectField(const FString& FieldName) const
{
if (!JsonObj.IsValid() || !JsonObj->HasTypedField<EJson::Object>(FieldName))
Expand Down
17 changes: 17 additions & 0 deletions Source/SIOJson/Public/SIOJsonObject.h
mikeseese marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ class SIOJSON_API USIOJsonObject : public UObject
//////////////////////////////////////////////////////////////////////////
// FJsonObject API Helpers (easy to use with simple Json objects)

/** Attempt to get the field named FieldName as a number; returns false if it doesn't exist or the type is incorrect.
* Attn.!! float used instead of double to make the function blueprintable! */
UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
bool TryGetNumberField(const FString& FieldName, float& OutNumber) const;

/** Get the field named FieldName as a number. Ensures that the field is present and is of type Json number.
* Attn.!! float used instead of double to make the function blueprintable! */
UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
Expand All @@ -99,6 +104,10 @@ class SIOJSON_API USIOJsonObject : public UObject
UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
void SetNumberField(const FString& FieldName, float Number);

/** Attempt to get the field named FieldName as a string; returns false if it doesn't exist or the type is incorrect. */
UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
bool TryGetStringField(const FString& FieldName, FString& OutString) const;

/** Get the field named FieldName as a string. */
UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
FString GetStringField(const FString& FieldName) const;
Expand All @@ -107,6 +116,10 @@ class SIOJSON_API USIOJsonObject : public UObject
UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
void SetStringField(const FString& FieldName, const FString& StringValue);

/** Attempt to get the field named FieldName as a boolean; returns false if it doesn't exist or the type is incorrect. */
UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
bool TryGetBoolField(const FString& FieldName, bool& OutBool) const;

/** Get the field named FieldName as a boolean. */
UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
bool GetBoolField(const FString& FieldName) const;
Expand All @@ -115,6 +128,10 @@ class SIOJSON_API USIOJsonObject : public UObject
UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
void SetBoolField(const FString& FieldName, bool InValue);

/** Attempt to get the field named FieldName as an object; returns false if it doesn't exist or the type is incorrect. */
UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
bool TryGetObjectField(const FString& FieldName, USIOJsonObject*& OutObject) const;

/** Get the field named FieldName as a Json object. */
UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
USIOJsonObject* GetObjectField(const FString& FieldName) const;
Expand Down