Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
Fix #70 (Possible crash on empty field name)
Browse files Browse the repository at this point in the history
  • Loading branch information
ufna committed Aug 2, 2016
1 parent 4ac47ad commit a40bc5f
Showing 1 changed file with 31 additions and 50 deletions.
81 changes: 31 additions & 50 deletions Source/VaRestPlugin/Private/Json/VaRestJsonObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ TArray<FString> UVaRestJsonObject::GetFieldNames()

bool UVaRestJsonObject::HasField(const FString& FieldName) const
{
if (!JsonObj.IsValid())
if (!JsonObj.IsValid() || FieldName.IsEmpty())
{
return false;
}
Expand All @@ -113,7 +113,7 @@ bool UVaRestJsonObject::HasField(const FString& FieldName) const

void UVaRestJsonObject::RemoveField(const FString& FieldName)
{
if (!JsonObj.IsValid())
if (!JsonObj.IsValid() || FieldName.IsEmpty())
{
return;
}
Expand All @@ -123,22 +123,26 @@ void UVaRestJsonObject::RemoveField(const FString& FieldName)

UVaRestJsonValue* UVaRestJsonObject::GetField(const FString& FieldName) const
{
if (!JsonObj.IsValid())
if (!JsonObj.IsValid() || FieldName.IsEmpty())
{
return nullptr;
}

TSharedPtr<FJsonValue> NewVal = JsonObj->TryGetField(FieldName);
if (NewVal.IsValid())
{
UVaRestJsonValue* NewValue = NewObject<UVaRestJsonValue>();
NewValue->SetRootValue(NewVal);

UVaRestJsonValue* NewValue = NewObject<UVaRestJsonValue>();
NewValue->SetRootValue(NewVal);

return NewValue;
return NewValue;
}
return nullptr;
}

void UVaRestJsonObject::SetField(const FString& FieldName, UVaRestJsonValue* JsonValue)
{
if (!JsonObj.IsValid())
if (!JsonObj.IsValid() || FieldName.IsEmpty())
{
return;
}
Expand All @@ -152,22 +156,18 @@ void UVaRestJsonObject::SetField(const FString& FieldName, UVaRestJsonValue* Jso

float UVaRestJsonObject::GetNumberField(const FString& FieldName) const
{
if (!JsonObj->HasTypedField<EJson::Number>(FieldName))
if (!JsonObj.IsValid() || !JsonObj->HasTypedField<EJson::Number>(FieldName))
{
UE_LOG(LogVaRest, Warning, TEXT("No field with name %s of type Number"), *FieldName);
}

if (!JsonObj.IsValid())
{
return 0.0f;
}
}

return JsonObj->GetNumberField(FieldName);
}

void UVaRestJsonObject::SetNumberField(const FString& FieldName, float Number)
{
if (!JsonObj.IsValid())
if (!JsonObj.IsValid() || FieldName.IsEmpty())
{
return;
}
Expand All @@ -177,13 +177,9 @@ void UVaRestJsonObject::SetNumberField(const FString& FieldName, float Number)

FString UVaRestJsonObject::GetStringField(const FString& FieldName) const
{
if (!JsonObj->HasTypedField<EJson::String>(FieldName))
if (!JsonObj.IsValid() || !JsonObj->HasTypedField<EJson::String>(FieldName))
{
UE_LOG(LogVaRest, Warning, TEXT("No field with name %s of type String"), *FieldName);
}

if (!JsonObj.IsValid())
{
return TEXT("");
}

Expand All @@ -192,7 +188,7 @@ FString UVaRestJsonObject::GetStringField(const FString& FieldName) const

void UVaRestJsonObject::SetStringField(const FString& FieldName, const FString& StringValue)
{
if (!JsonObj.IsValid())
if (!JsonObj.IsValid() || FieldName.IsEmpty())
{
return;
}
Expand All @@ -202,13 +198,9 @@ void UVaRestJsonObject::SetStringField(const FString& FieldName, const FString&

bool UVaRestJsonObject::GetBoolField(const FString& FieldName) const
{
if (!JsonObj->HasTypedField<EJson::Boolean>(FieldName))
if (!JsonObj.IsValid() || !JsonObj->HasTypedField<EJson::Boolean>(FieldName))
{
UE_LOG(LogVaRest, Warning, TEXT("No field with name %s of type Boolean"), *FieldName);
}

if (!JsonObj.IsValid())
{
return false;
}

Expand All @@ -217,7 +209,7 @@ bool UVaRestJsonObject::GetBoolField(const FString& FieldName) const

void UVaRestJsonObject::SetBoolField(const FString& FieldName, bool InValue)
{
if (!JsonObj.IsValid())
if (!JsonObj.IsValid() || FieldName.IsEmpty())
{
return;
}
Expand All @@ -233,7 +225,7 @@ TArray<UVaRestJsonValue*> UVaRestJsonObject::GetArrayField(const FString& FieldN
}

TArray<UVaRestJsonValue*> OutArray;
if (!JsonObj.IsValid())
if (!JsonObj.IsValid() || FieldName.IsEmpty())
{
return OutArray;
}
Expand All @@ -252,7 +244,7 @@ TArray<UVaRestJsonValue*> UVaRestJsonObject::GetArrayField(const FString& FieldN

void UVaRestJsonObject::SetArrayField(const FString& FieldName, const TArray<UVaRestJsonValue*>& InArray)
{
if (!JsonObj.IsValid())
if (!JsonObj.IsValid() || FieldName.IsEmpty())
{
return;
}
Expand Down Expand Up @@ -318,13 +310,9 @@ void UVaRestJsonObject::MergeJsonObject(UVaRestJsonObject* InJsonObject, bool Ov

UVaRestJsonObject* UVaRestJsonObject::GetObjectField(const FString& FieldName) const
{
if (!JsonObj->HasTypedField<EJson::Object>(FieldName))
if (!JsonObj.IsValid() || !JsonObj->HasTypedField<EJson::Object>(FieldName))
{
UE_LOG(LogVaRest, Warning, TEXT("No field with name %s of type Object"), *FieldName);
}

if (!JsonObj.IsValid())
{
return nullptr;
}

Expand All @@ -338,7 +326,7 @@ UVaRestJsonObject* UVaRestJsonObject::GetObjectField(const FString& FieldName) c

void UVaRestJsonObject::SetObjectField(const FString& FieldName, UVaRestJsonObject* JsonObject)
{
if (!JsonObj.IsValid())
if (!JsonObj.IsValid() || FieldName.IsEmpty())
{
return;
}
Expand All @@ -358,8 +346,7 @@ TArray<float> UVaRestJsonObject::GetNumberArrayField(const FString& FieldName)
}

TArray<float> NumberArray;

if (!JsonObj.IsValid())
if (!JsonObj.IsValid() || FieldName.IsEmpty())
{
return NumberArray;
}
Expand All @@ -381,7 +368,7 @@ TArray<float> UVaRestJsonObject::GetNumberArrayField(const FString& FieldName)

void UVaRestJsonObject::SetNumberArrayField(const FString& FieldName, const TArray<float>& NumberArray)
{
if (!JsonObj.IsValid())
if (!JsonObj.IsValid() || FieldName.IsEmpty())
{
return;
}
Expand All @@ -404,8 +391,7 @@ TArray<FString> UVaRestJsonObject::GetStringArrayField(const FString& FieldName)
}

TArray<FString> StringArray;

if (!JsonObj.IsValid())
if (!JsonObj.IsValid() || FieldName.IsEmpty())
{
return StringArray;
}
Expand All @@ -427,13 +413,12 @@ TArray<FString> UVaRestJsonObject::GetStringArrayField(const FString& FieldName)

void UVaRestJsonObject::SetStringArrayField(const FString& FieldName, const TArray<FString>& StringArray)
{
if (!JsonObj.IsValid())
if (!JsonObj.IsValid() || FieldName.IsEmpty())
{
return;
}

TArray< TSharedPtr<FJsonValue> > EntriesArray;

for (auto String : StringArray)
{
EntriesArray.Add(MakeShareable(new FJsonValueString(String)));
Expand All @@ -450,8 +435,7 @@ TArray<bool> UVaRestJsonObject::GetBoolArrayField(const FString& FieldName)
}

TArray<bool> BoolArray;

if (!JsonObj.IsValid())
if (!JsonObj.IsValid() || FieldName.IsEmpty())
{
return BoolArray;
}
Expand All @@ -473,13 +457,12 @@ TArray<bool> UVaRestJsonObject::GetBoolArrayField(const FString& FieldName)

void UVaRestJsonObject::SetBoolArrayField(const FString& FieldName, const TArray<bool>& BoolArray)
{
if (!JsonObj.IsValid())
if (!JsonObj.IsValid() || FieldName.IsEmpty())
{
return;
}

TArray< TSharedPtr<FJsonValue> > EntriesArray;

for (auto Boolean : BoolArray)
{
EntriesArray.Add(MakeShareable(new FJsonValueBoolean(Boolean)));
Expand All @@ -496,8 +479,7 @@ TArray<UVaRestJsonObject*> UVaRestJsonObject::GetObjectArrayField(const FString&
}

TArray<UVaRestJsonObject*> OutArray;

if (!JsonObj.IsValid())
if (!JsonObj.IsValid() || FieldName.IsEmpty())
{
return OutArray;
}
Expand All @@ -523,13 +505,12 @@ TArray<UVaRestJsonObject*> UVaRestJsonObject::GetObjectArrayField(const FString&

void UVaRestJsonObject::SetObjectArrayField(const FString& FieldName, const TArray<UVaRestJsonObject*>& ObjectArray)
{
if (!JsonObj.IsValid())
if (!JsonObj.IsValid() || FieldName.IsEmpty())
{
return;
}

TArray< TSharedPtr<FJsonValue> > EntriesArray;

for (auto Value : ObjectArray)
{
EntriesArray.Add(MakeShareable(new FJsonValueObject(Value->GetRootObject())));
Expand Down

0 comments on commit a40bc5f

Please sign in to comment.