MetaSoul

Loading...
Searching...
No Matches
ESJsonValue.h
Go to the documentation of this file.
1#pragma once
2
3#include "Runtime/Json/Public/Dom/JsonValue.h"
4#include "Runtime/Core/Public/Misc/Base64.h"
5#include "Runtime/Core/Public/Templates/SharedPointer.h"
6#include "ESJsonValue.generated.h"
7
8 class UESJsonObject;
9
10UENUM(BlueprintType)
11namespace EESJson
12{
13 enum Type
14 {
15 None,
16 Null,
17 String,
18 Number,
19 Boolean,
20 Array,
21 Object,
22 Binary
23 };
24}
25
26 class EMOSHAPESUBSYSTEM_API FJsonValueBinary : public FJsonValue
27{
28 public:
29 FJsonValueBinary(const TArray<uint8>& InBinary) : Value(InBinary) { Type = EJson::String; } //pretends to be none
30
31 virtual bool TryGetString(FString& OutString) const override
32 {
33 //OutString = FString::FromHexBlob(Value.GetData(), Value.Num()); //HEX encoding
34 OutString = FBase64::Encode(Value); //Base64 encoding
35 return true;
36 }
37 virtual bool TryGetNumber(double& OutDouble) const override
38 {
39 OutDouble = Value.Num();
40 return true;
41 }
42
43 //hackery: we use this as an indicator we have a binary (strings don't normally do this)
44 virtual bool TryGetBool(bool& OutBool) const override { return false; }
45
47 TArray<uint8> AsBinary() { return Value; }
48
50 static bool IsBinary(const TSharedPtr<FJsonValue>& InJsonValue);
51
53 static TArray<uint8> AsBinary(const TSharedPtr<FJsonValue>& InJsonValue);
54
55 protected:
56 TArray<uint8> Value;
57
58 virtual FString GetType() const override { return TEXT("Binary"); }
59};
60
61UCLASS(Blueprintable, BlueprintType)
62class EMOSHAPESUBSYSTEM_API UESJsonValue : public UObject
63{
64 GENERATED_UCLASS_BODY()
65 public:
66
69 UFUNCTION(BlueprintPure, meta = (DisplayName = "Construct Json Number Value", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "EmoShapeJson|Json")
70 static UESJsonValue* ConstructJsonValueNumber(UObject* WorldContextObject, float Number);
71
73 UFUNCTION(BlueprintPure, meta = (DisplayName = "Construct Json String Value", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "EmoShapeJson|Json")
74 static UESJsonValue* ConstructJsonValueString(UObject* WorldContextObject, const FString& StringValue);
75
77 UFUNCTION(BlueprintPure, meta = (DisplayName = "Construct Json Bool Value", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "EmoShapeJson|Json")
78 static UESJsonValue* ConstructJsonValueBool(UObject* WorldContextObject, bool InValue);
79
81 UFUNCTION(BlueprintPure, meta = (DisplayName = "Construct Json Array Value", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "EmoShapeJson|Json")
82 static UESJsonValue* ConstructJsonValueArray(UObject* WorldContextObject, const TArray<UESJsonValue*>& InArray);
83
85 UFUNCTION(BlueprintPure, meta = (DisplayName = "Construct Json Object Value", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "EmoShapeJson|Json")
86 static UESJsonValue* ConstructJsonValueObject(UESJsonObject *JsonObject, UObject* WorldContextObject);
87
89 UFUNCTION(BlueprintPure, meta = (DisplayName = "Construct Json Binary Value", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "EmoShapeJson|Json")
90 static UESJsonValue* ConstructJsonValueBinary(UObject* WorldContextObject, TArray<uint8> ByteArray);
91
93 static UESJsonValue* ConstructJsonValue(UObject* WorldContextObject, const TSharedPtr<FJsonValue>& InValue);
94
96 UFUNCTION(BlueprintPure, meta = (DisplayName = "Value From Json String", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "EmoShapeJson|Json")
97 static UESJsonValue* ValueFromJsonString(UObject* WorldContextObject, const FString& StringValue);
98
100 TSharedPtr<FJsonValue>& GetRootValue();
101
103 void SetRootValue(TSharedPtr<FJsonValue>& JsonValue);
104
105
107 // FJsonValue API
108
110 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Json")
111 EESJson::Type GetType() const;
112
114 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Json")
115 FString GetTypeString() const;
116
118 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Json")
119 bool IsNull() const;
120
123 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Json")
124 float AsNumber() const;
125
127 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Json")
128 FString AsString() const;
129
131 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Json")
132 bool AsBool() const;
133
135 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Json")
136 TArray<UESJsonValue*> AsArray() const;
137
139 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Json")
140 UESJsonObject* AsObject();
141
142 //Convert message to binary data
143 UFUNCTION(BlueprintPure, Category = "EmoShapeJson|Json")
144 TArray<uint8> AsBinary();
145
146 UFUNCTION(BlueprintCallable, Category = "EmoShapeJson|Json")
147 FString EncodeJson() const;
148
150 // Data
151
152 private:
154 TSharedPtr<FJsonValue> JsonVal;
155
156
158 // Helpers
159
160 protected:
162 void ErrorMessage(const FString& InType) const;
163};
FJsonValueBinary
Definition ESJsonValue.h:27
FJsonValueBinary::AsBinary
TArray< uint8 > AsBinary()
Definition ESJsonValue.h:47
FJsonValueBinary::FJsonValueBinary
FJsonValueBinary(const TArray< uint8 > &InBinary)
Definition ESJsonValue.h:29
FJsonValueBinary::TryGetNumber
virtual bool TryGetNumber(double &OutDouble) const override
Definition ESJsonValue.h:37
FJsonValueBinary::TryGetString
virtual bool TryGetString(FString &OutString) const override
Definition ESJsonValue.h:31
FJsonValueBinary::TryGetBool
virtual bool TryGetBool(bool &OutBool) const override
Definition ESJsonValue.h:44
FJsonValueBinary::GetType
virtual FString GetType() const override
Definition ESJsonValue.h:58
FJsonValueBinary::Value
TArray< uint8 > Value
Definition ESJsonValue.h:56
UESJsonObject
Definition ESJsonObject.h:10
UESJsonValue
Definition ESJsonValue.h:63
EESJson
Definition ESJsonValue.h:12
EESJson::Type
Type
Definition ESJsonValue.h:14
EESJson::Object
@ Object
Definition ESJsonValue.h:21
EESJson::Boolean
@ Boolean
Definition ESJsonValue.h:19
EESJson::None
@ None
Definition ESJsonValue.h:15
EESJson::String
@ String
Definition ESJsonValue.h:17
EESJson::Array
@ Array
Definition ESJsonValue.h:20
EESJson::Number
@ Number
Definition ESJsonValue.h:18
EESJson::Binary
@ Binary
Definition ESJsonValue.h:22
EESJson::Null
@ Null
Definition ESJsonValue.h:16