123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615 |
- // Copyright 2024 Tracer Interactive, LLC. All Rights Reserved.
- #include "MatureJsonValue.h"
- #include "MatureJsonObject.h"
- #include "MatureJsonList.h"
- #include "MatureJsonLog.h"
- #include "json_cast.hpp"
- #include "Misc/FileHelper.h"
- #include "Policies/CondensedJsonPrintPolicy.h"
- #include "Policies/PrettyJsonPrintPolicy.h"
- FMatureJsonValue::ValueWrap::ValueWrap()
- : Document(MakeShared<mature::Document>())
- , ValueRef(*Document.Get())
- {
- }
- FMatureJsonValue::ValueWrap::ValueWrap(TSharedPtr<mature::Document> Doc)
- : Document(Doc)
- , ValueRef(*Document.Get())
- {
- }
- FMatureJsonValue::ValueWrap::ValueWrap(TSharedPtr<mature::Document> Doc, mature::Value& Ref)
- : Document(Doc)
- , ValueRef(Ref)
- {
- }
- bool FMatureJsonValue::ValueWrap::IsRoot() {
- void* Doc = Document.Get();
- void* ValPtr = &ValueRef;
- return Doc == ValPtr;
- }
- FMatureJsonValue::FMatureJsonValue(/*const TSharedPtr<mature::Document>& Doc*/)
- : ValueCache(MakeShared<ValueWrap>())
- {
- }
- FMatureJsonValue::FMatureJsonValue(const TSharedPtr<mature::Document>& Doc, mature::Value& v)
- : ValueCache(MakeShared<ValueWrap>(Doc, v))
- {
- }
- FMatureJsonValue::FMatureJsonValue(const FMatureJsonValue& rhs)
- : ValueCache(rhs.ValueCache)
- {
- }
- FMatureJsonValue& FMatureJsonValue::SetValue() {
- ValueRef().SetNull();
- return *this;
- }
- FMatureJsonValue& FMatureJsonValue::SetValue(const bool Value )
- {
- ValueRef().SetBool(Value);
- return *this;
- }
- FMatureJsonValue& FMatureJsonValue::SetValue(const float Value )
- {
- ValueRef().SetFloat(Value);
- return *this;
- }
- FMatureJsonValue& FMatureJsonValue::SetValue(const double Value )
- {
- ValueRef().SetDouble(Value);
- return *this;
- }
- FMatureJsonValue& FMatureJsonValue::SetValue(const int8 Value )
- {
- ValueRef().SetInt(Value);
- return *this;
- }
- FMatureJsonValue& FMatureJsonValue::SetValue(const uint8 Value )
- {
- ValueRef().SetUint(Value);
- return *this;
- }
- FMatureJsonValue& FMatureJsonValue::SetValue(const int16 Value )
- {
- ValueRef().SetInt(Value);
- return *this;
- }
- FMatureJsonValue& FMatureJsonValue::SetValue(const uint16 Value )
- {
- ValueRef().SetUint(Value);
- return *this;
- }
- FMatureJsonValue& FMatureJsonValue::SetValue(const int32 Value )
- {
- ValueRef().SetInt(Value);
- return *this;
- }
- FMatureJsonValue& FMatureJsonValue::SetValue(const uint32 Value )
- {
- ValueRef().SetUint(Value);
- return *this;
- }
- FMatureJsonValue& FMatureJsonValue::SetValue(const int64 Value )
- {
- ValueRef().SetInt64(Value);
- return *this;
- }
- FMatureJsonValue& FMatureJsonValue::SetValue(const uint64 Value )
- {
- ValueRef().SetUint64(Value);
- return *this;
- }
- FMatureJsonValue& FMatureJsonValue::SetValue(const TCHAR* Value)
- {
- FString in = Value;
- ValueRef().SetString(*in, in.Len(), GetAllocator());
- return *this;
- }
- FMatureJsonValue& FMatureJsonValue::SetValue( const FString& Value )
- {
- ValueRef().SetString(*Value, Value.Len(), GetAllocator());
- return *this;
- }
- FMatureJsonValue& FMatureJsonValue::SetValue( const FDateTime& Value)
- {
- FString serial = Value.ToIso8601();
- ValueRef().SetString(*serial, serial.Len(), GetAllocator());
- return *this;
- }
- FMatureJsonValue& FMatureJsonValue::SetValue( const FGuid& Value )
- {
- FString serial = Value.ToString(EGuidFormats::DigitsWithHyphens);
- ValueRef().SetString(*serial, serial.Len(), GetAllocator());
- return *this;
- }
- FMatureJsonValue& FMatureJsonValue::SetValue( const FColor& Value )
- {
- //
- FString serial = "#" + Value.ToHex();
- ValueRef().SetString(*serial, serial.Len(), GetAllocator());
- return *this;
- }
- FMatureJsonValue& FMatureJsonValue::SetValue(const FTransform& Value)
- {
- //
- ToObject().SetValue(Value);
- return *this;
- }
- FMatureJsonValue& FMatureJsonValue::SetValue(const FVector& Value)
- {
- //
- ToObject().SetValue(Value);
- return *this;
- }
- FMatureJsonValue& FMatureJsonValue::SetValue(const FRotator& Value)
- {
- //
- ToObject().SetValue(Value);
- return *this;
- }
- FMatureJsonValue& FMatureJsonValue::SetValue(const FLinearColor& Value)
- {
- ToObject().SetValue(Value);
- return *this;
- }
- FMatureJsonValue& FMatureJsonValue::SetValue(const FMatureJsonValue& Value) {
- ValueRef().CopyFrom(Value.ValueRef(), GetAllocator());
- return *this;
- }
- FMatureJsonValue& FMatureJsonValue::SetValue(const FMatureJsonObject& Value) {
- ToObject().SetValue(Value);
- return *this;
- }
- FMatureJsonValue& FMatureJsonValue::SetValue(const FMatureJsonList& Value) {
- if (ValueRef().GetType()!=mature::Type::kArrayType)ValueRef().SetArray();
- FMatureJsonList(Document(), ValueRef().GetArray()).AddValue(Value);
- return *this;
- }
- FMatureJsonValue& FMatureJsonValue::SetValue(mature::Value& Value) {
- ValueRef().CopyFrom(Value,GetAllocator());
- return *this;
- }
- EMatureJsonTypeNumber FMatureJsonValue::NumberType()const {
- if (mature::Type::kNumberType != ValueRef().GetType())return EMatureJsonTypeNumber::Unkown;
- if (ValueRef().IsDouble())return EMatureJsonTypeNumber::Double;
- else if (ValueRef().IsInt64())return EMatureJsonTypeNumber::Int64;
- else if (ValueRef().IsUint64())return EMatureJsonTypeNumber::Uint64;
- else if (ValueRef().IsInt())return EMatureJsonTypeNumber::Int32;
- else if (ValueRef().IsUint())return EMatureJsonTypeNumber::Uint32;
- return EMatureJsonTypeNumber::Unkown;
- }
- EMatureJsonType FMatureJsonValue::GetType() const
- {
- switch (ValueRef().GetType())
- {
- case mature::Type::kNullType: return EMatureJsonType::Null;
- case mature::Type::kFalseType : return EMatureJsonType::Bool;
- case mature::Type::kTrueType: return EMatureJsonType::Bool;
- case mature::Type::kNumberType: return EMatureJsonType::Number;
- case mature::Type::kStringType: return EMatureJsonType::String;
- case mature::Type::kObjectType: return EMatureJsonType::Object;
- case mature::Type::kArrayType: return EMatureJsonType::Array;
- }
- return EMatureJsonType::Null;
- }
- int FMatureJsonValue::Size() const {
- return ValueRef().Size();
- }
- bool FMatureJsonValue::IsEmpty() const {
- return ValueRef().Empty();
- }
- void FMatureJsonValue::Clear() {
- ValueRef().Clear();
- }
- bool FMatureJsonValue::ToBoolean() const
- {
- switch (ValueRef().GetType())
- {
- case mature::Type::kFalseType: return ValueRef().GetBool();
- case mature::Type::kTrueType: return ValueRef().GetBool();
- case mature::Type::kNumberType: return mature::GetNumber<bool>(ValueRef());
- case mature::Type::kStringType: return FString(ValueRef().GetString()).ToBool();
- }
- return false;
- }
- float FMatureJsonValue::ToFloat() const
- {
- return mature::GetNumber<float>(ValueRef());
- }
- double FMatureJsonValue::ToNumber() const
- {
- return mature::GetNumber<double>(ValueRef());
- }
- int32 FMatureJsonValue::ToInteger() const
- {
- return mature::GetNumber<int32>(ValueRef());
- }
- FString FMatureJsonValue::ToString() const
- {
- switch (ValueRef().GetType())
- {
- case mature::Type::kFalseType: return TEXT("false");
- case mature::Type::kTrueType: return TEXT("true");
- case mature::Type::kNumberType: return FString::SanitizeFloat(mature::GetNumber<float>(ValueRef()), 0);
- case mature::Type::kStringType: return FString(ValueRef().GetString());
- }
- return FString();
- }
- FDateTime FMatureJsonValue::ToDateTime() const
- {
- if (mature::Type::kStringType != ValueRef().GetType())
- return FDateTime();
- FDateTime DateTime;
- if (FDateTime::ParseIso8601(ValueRef().GetString(), DateTime))
- return DateTime;
- return FDateTime();
- }
- FGuid FMatureJsonValue::ToGuid() const
- {
- if (mature::Type::kStringType != ValueRef().GetType())
- return FGuid();
- FGuid Guid;
- if (FGuid::Parse(ValueRef().GetString(), Guid))
- return Guid;
- return FGuid();
- }
- FLinearColor FMatureJsonValue::ToLinearColor() const {
- FLinearColor value;
- if (ValueRef().GetType() != mature::Type::kObjectType)return value;
- ToObject().GetValue(value);
- return value;
- }
- FColor FMatureJsonValue::ToColor() const
- {
- if (mature::Type::kStringType != ValueRef().GetType())
- return FColor();
- if (IsColor(ValueRef().GetString()))
- return FColor::FromHex(ValueRef().GetString());
- return FColor();
- }
- FMatureJsonObject FMatureJsonValue::ToObject(bool check) const
- {
- if (ValueRef().GetType() != mature::Type::kObjectType)
- {
- if (!(check == false || ValueRef().GetType() == mature::Type::kNullType)) {
- return FMatureJsonObject();
- }
- ValueRef().SetObject();
- }
- return FMatureJsonObject(Document(), ValueRef().GetObject());
- }
- FMatureJsonList FMatureJsonValue::ToList(bool check) const
- {
- if (ValueRef().GetType() != mature::Type::kArrayType)
- {
- if (!(check == false || ValueRef().GetType() == mature::Type::kNullType)) {
- return FMatureJsonList();
- }
- ValueRef().SetArray();
- }
- return FMatureJsonList(Document(), ValueRef().GetArray());
- }
- int32 FMatureJsonValue::ToInt32() const
- {
- return mature::GetNumber<int32>(ValueRef());
- }
- uint32 FMatureJsonValue::ToUInt32() const
- {
- return mature::GetNumber<uint32>(ValueRef());
- }
- int64 FMatureJsonValue::ToInt64() const
- {
- return mature::GetNumber<int64>(ValueRef());
- }
- uint64 FMatureJsonValue::ToUInt64() const
- {
- return mature::GetNumber<uint64>(ValueRef());
- }
- bool FMatureJsonValue::GetValue(bool& value)const {
- if (mature::Type::kNumberType != ValueRef().GetType())return false;
- if (ValueRef().IsDouble())value = 0.05f <= ValueRef().GetDouble() || (ValueRef().GetDouble()) <= -0.05f;
- else if (ValueRef().IsInt64())value = bool (ValueRef().GetInt64());
- else if (ValueRef().IsUint64())value = bool(ValueRef().GetUint64());
- else if (ValueRef().IsInt())value = bool(ValueRef().GetInt());
- else if (ValueRef().IsUint())value = bool(ValueRef().GetUint());
- else return false;
- return false;
- //return GetNumberBool<bool>(ValueRef(), value);
- }
- bool FMatureJsonValue::GetValue(float& value)const {
- if (mature::Type::kNumberType != ValueRef().GetType())return false;
- if (ValueRef().IsDouble())value = (ValueRef().GetDouble());
- else if (ValueRef().IsInt64())value = (ValueRef().GetInt64());
- else if (ValueRef().IsUint64())value = (ValueRef().GetUint64());
- else if (ValueRef().IsInt())value = (ValueRef().GetInt());
- else if (ValueRef().IsUint())value = (ValueRef().GetUint());
- else return false;
- return false;
- }
- bool FMatureJsonValue::GetValue(int32& value)const {
- if (mature::Type::kNumberType != ValueRef().GetType())return false;
- if (ValueRef().IsDouble())value = (ValueRef().GetDouble());
- else if (ValueRef().IsInt64())value = (ValueRef().GetInt64());
- else if (ValueRef().IsUint64())value = (ValueRef().GetUint64());
- else if (ValueRef().IsInt())value = (ValueRef().GetInt());
- else if (ValueRef().IsUint())value = (ValueRef().GetUint());
- else return false;
- return false;
- }
- bool FMatureJsonValue::GetValue(int64& value)const {
- if (mature::Type::kNumberType != ValueRef().GetType())return false;
- if (ValueRef().IsDouble())value = (ValueRef().GetDouble());
- else if (ValueRef().IsInt64())value = (ValueRef().GetInt64());
- else if (ValueRef().IsUint64())value = (ValueRef().GetUint64());
- else if (ValueRef().IsInt())value = (ValueRef().GetInt());
- else if (ValueRef().IsUint())value = (ValueRef().GetUint());
- else return false;
- return false;
- }
- bool FMatureJsonValue::GetValue(uint32& value)const {
- if (mature::Type::kNumberType != ValueRef().GetType())return false;
- if (ValueRef().IsDouble())value = (ValueRef().GetDouble());
- else if (ValueRef().IsInt64())value = (ValueRef().GetInt64());
- else if (ValueRef().IsUint64())value = (ValueRef().GetUint64());
- else if (ValueRef().IsInt())value = (ValueRef().GetInt());
- else if (ValueRef().IsUint())value = (ValueRef().GetUint());
- else return false;
- return false;
- }
- bool FMatureJsonValue::GetValue(uint64& value)const {
- if (mature::Type::kNumberType != ValueRef().GetType())return false;
- if (ValueRef().IsDouble())value = (ValueRef().GetDouble());
- else if (ValueRef().IsInt64())value = (ValueRef().GetInt64());
- else if (ValueRef().IsUint64())value = (ValueRef().GetUint64());
- else if (ValueRef().IsInt())value = (ValueRef().GetInt());
- else if (ValueRef().IsUint())value = (ValueRef().GetUint());
- else return false;
- return false;
- }
- bool FMatureJsonValue::GetValue(double& value)const {
- if (mature::Type::kNumberType != ValueRef().GetType())return false;
- if (ValueRef().IsDouble())value = (ValueRef().GetDouble());
- else if (ValueRef().IsInt64())value = (ValueRef().GetInt64());
- else if (ValueRef().IsUint64())value = (ValueRef().GetUint64());
- else if (ValueRef().IsInt())value = (ValueRef().GetInt());
- else if (ValueRef().IsUint())value = (ValueRef().GetUint());
- else return false;
- return false;
- }
- bool FMatureJsonValue::GetValue(FString& value)const {
- return GetStringBool(ValueRef(), value);
- }
- bool FMatureJsonValue::GetValue(FDateTime& value)const {
- if (mature::Type::kStringType != ValueRef().GetType())
- return false;
- return FDateTime::ParseIso8601(ValueRef().GetString(), value);
- }
- bool FMatureJsonValue::GetValue(FGuid& value)const {
- if (mature::Type::kStringType != ValueRef().GetType())
- return false;
- if (FGuid::Parse(ValueRef().GetString(), value))
- return value.IsValid();
- return false;
- }
- bool FMatureJsonValue::GetValue(FColor& value)const {
- if (mature::Type::kStringType != ValueRef().GetType())
- return false;
- if (!IsColor(ValueRef().GetString()))
- return false;
- value = FColor::FromHex(ValueRef().GetString());
- return true;
- }
- bool FMatureJsonValue::GetValue(FLinearColor& value)const {
- if (ValueRef().GetType() != mature::Type::kObjectType)return false;;
- ToObject().GetValue(value);
- return true;
- }
- bool FMatureJsonValue::GetValue(FRotator& value)const {
- if (mature::Type::kObjectType != ValueRef().GetType())
- return false;
- ToObject().GetValue(value);
- return true;
- }
- bool FMatureJsonValue::GetValue(FVector& value)const {
- if (mature::Type::kObjectType != ValueRef().GetType())
- return false;
- ToObject().GetValue(value);
- return true;
- }
- bool FMatureJsonValue::GetValue(FTransform& value)const {
- if (mature::Type::kObjectType != ValueRef().GetType())
- return false;
- ToObject().GetValue(value);
- return true;
- }
- bool FMatureJsonValue::IsDateTime() const
- {
- if (mature::Type::kStringType != ValueRef().GetType())
- return false;
- FDateTime DateTime;
- return FDateTime::ParseIso8601(ValueRef().GetString(), DateTime );
- }
- bool FMatureJsonValue::IsGuid() const
- {
- if (mature::Type::kStringType != ValueRef().GetType())
- return false;
- FGuid Guid;
- if ( FGuid::Parse(ValueRef().GetString(), Guid ) )
- return Guid.IsValid();
- return false;
- }
- bool FMatureJsonValue::IsColor(FString hex_string) const
- {
- if (hex_string.IsEmpty() )
- return false;
- if (hex_string.StartsWith(TEXT("#"))) {
- hex_string.RemoveAt(0, 1);
- }
- int32 StartIndex = 0;
- if (hex_string.Len() == 8 )
- {
- for (int32 i = 0; i < 8; i++)
- if (!FChar::IsHexDigit(hex_string[StartIndex++]))
- return false;
- return true;
- }
- if (hex_string.Len() == 6 )
- {
- for (int32 i = 0; i < 6; i++)
- if (!FChar::IsHexDigit(hex_string[StartIndex++]))
- return false;
- return true;
- }
- if (hex_string.Len() == 3 )
- {
- for ( int32 i = 0; i < 3; i++ )
- if ( !FChar::IsHexDigit(hex_string[ StartIndex++ ] ) )
- return false;
- return true;
- }
- return false;
- }
- FMatureJsonValue& FMatureJsonValue::operator=(const FMatureJsonValue& rhs) {
- ValueCache = rhs.ValueCache;
- return *this;
- }
- bool FMatureJsonValue::Parse(const FString& Text) {
- ValueCache = MakeShared<ValueWrap>();
- Document()->Parse(*Text);
- if (!Document()->HasParseError())return true;
- int offset = Document()->GetErrorOffset();
- rapidjson::ParseErrorCode errorCode = Document()->GetParseError();
- FString ErrorPos;
- if (offset < Text.Len() && errorCode != rapidjson::ParseErrorCode::kParseErrorDocumentEmpty) {
- ErrorPos = FString((*Text) + offset, 20);
- }
- FString Desc = UTF8_TO_TCHAR( rapidjson::GetParseError_En(errorCode));
- UE_LOG(MatureJsonLog,Log,TEXT("Parse Error: ErrorCode[%d] Desc[%s] pos[%d] Content[%s]")
- , errorCode , *Desc, offset, *ErrorPos
- );
- return false;
- }
- bool FMatureJsonValue::ParseFile(const FString& FileName) {
- FString FileContents;
- if (!FFileHelper::LoadFileToString(FileContents, *FileName)) {
- return false;
- }
- return Parse(FileContents);
- }
- FString FMatureJsonValue::SaveString() const{
- mature::StringBuffer buffer;
- mature::Writer writer(buffer);
- TSharedPtr<ValueWrap> DocRoot;
- if (ValueCache->IsRoot()) {
- DocRoot = ValueCache;
- }
- else {
- DocRoot = MakeShared<ValueWrap>();
- DocRoot->ValueRef.CopyFrom(ValueCache->ValueRef, DocRoot->Document->GetAllocator());
- }
- if (!DocRoot->Document->Accept(writer))
- return FString();
- return buffer.GetString();
- }
- bool FMatureJsonValue::SaveFile(FString FileName) const {
- return FFileHelper::SaveStringToFile(SaveString(), *FileName);
- }
- TSharedPtr<mature::Document> FMatureJsonValue::Document() {
- return ValueCache->Document;
- }
- TSharedPtr<mature::Document> FMatureJsonValue::Document() const {
- return ValueCache->Document;
- }
- inline mature::Value& FMatureJsonValue::ValueRef() {
- return ValueCache->ValueRef;
- }
- inline mature::Value& FMatureJsonValue::ValueRef() const {
- return ValueCache->ValueRef;
- }
- inline mature::Allocator& FMatureJsonValue::GetAllocator() {
- return ValueCache->Document->GetAllocator();
- }
- inline mature::Allocator& FMatureJsonValue::GetAllocator()const {
- return ValueCache->Document->GetAllocator();
- }
|