Skip to content

Commit

Permalink
add TryGet... methods to USIOJsonObject
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeseese committed Sep 11, 2024
1 parent f7722b8 commit a51c128
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
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
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 falls 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 falls 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 falls 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 falls 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

0 comments on commit a51c128

Please sign in to comment.