SIOJsonValue.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Modifications Copyright 2018-current Getnamo. All Rights Reserved
  2. // Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
  3. // Copyright 2014 Vladimir Alyamkin. All Rights Reserved.
  4. #pragma once
  5. #include "Runtime/Json/Public/Dom/JsonValue.h"
  6. #include "Runtime/Core/Public/Misc/Base64.h"
  7. #include "Runtime/Core/Public/Templates/SharedPointer.h"
  8. #include "SIOJsonValue.generated.h"
  9. class USIOJsonObject;
  10. /**
  11. * Represents all the types a Json Value can be.
  12. */
  13. UENUM(BlueprintType)
  14. namespace ESIOJson
  15. {
  16. enum Type
  17. {
  18. None,
  19. Null,
  20. String,
  21. Number,
  22. Boolean,
  23. Array,
  24. Object,
  25. Binary
  26. };
  27. }
  28. class SIOJSON_API FJsonValueBinary : public FJsonValue
  29. {
  30. public:
  31. FJsonValueBinary(const TArray<uint8>& InBinary) : Value(InBinary) { Type = EJson::String; } //pretends to be none
  32. virtual bool TryGetString(FString& OutString) const override
  33. {
  34. //OutString = FString::FromHexBlob(Value.GetData(), Value.Num()); //HEX encoding
  35. OutString = FBase64::Encode(Value); //Base64 encoding
  36. return true;
  37. }
  38. virtual bool TryGetNumber(double& OutDouble) const override
  39. {
  40. OutDouble = Value.Num();
  41. return true;
  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. /** Return our binary data from this value */
  46. TArray<uint8> AsBinary() { return Value; }
  47. /** Convenience method to determine if passed FJsonValue is a FJsonValueBinary or not. */
  48. static bool IsBinary(const TSharedPtr<FJsonValue>& InJsonValue);
  49. /** Convenience method to get binary array from unknown JsonValue, test with IsBinary first. */
  50. static TArray<uint8> AsBinary(const TSharedPtr<FJsonValue>& InJsonValue);
  51. protected:
  52. TArray<uint8> Value;
  53. virtual FString GetType() const override { return TEXT("Binary"); }
  54. };
  55. /**
  56. * Blueprintable FJsonValue wrapper
  57. */
  58. UCLASS(BlueprintType, Blueprintable)
  59. class SIOJSON_API USIOJsonValue : public UObject
  60. {
  61. GENERATED_UCLASS_BODY()
  62. public:
  63. /** Create new Json Number value
  64. * Attn.!! float used instead of double to make the function blueprintable! */
  65. UFUNCTION(BlueprintPure, meta = (DisplayName = "Construct Json Number Value", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "SIOJ|Json")
  66. static USIOJsonValue* ConstructJsonValueNumber(UObject* WorldContextObject, float Number);
  67. /** Create new Json String value */
  68. UFUNCTION(BlueprintPure, meta = (DisplayName = "Construct Json String Value", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "SIOJ|Json")
  69. static USIOJsonValue* ConstructJsonValueString(UObject* WorldContextObject, const FString& StringValue);
  70. /** Create new Json Bool value */
  71. UFUNCTION(BlueprintPure, meta = (DisplayName = "Construct Json Bool Value", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "SIOJ|Json")
  72. static USIOJsonValue* ConstructJsonValueBool(UObject* WorldContextObject, bool InValue);
  73. /** Create new Json Array value */
  74. UFUNCTION(BlueprintPure, meta = (DisplayName = "Construct Json Array Value", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "SIOJ|Json")
  75. static USIOJsonValue* ConstructJsonValueArray(UObject* WorldContextObject, const TArray<USIOJsonValue*>& InArray);
  76. /** Create new Json Object value */
  77. UFUNCTION(BlueprintPure, meta = (DisplayName = "Construct Json Object Value", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "SIOJ|Json")
  78. static USIOJsonValue* ConstructJsonValueObject(USIOJsonObject *JsonObject, UObject* WorldContextObject);
  79. /** Create new Json Binary value */
  80. UFUNCTION(BlueprintPure, meta = (DisplayName = "Construct Json Binary Value", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "SIOJ|Json")
  81. static USIOJsonValue* ConstructJsonValueBinary(UObject* WorldContextObject, TArray<uint8> ByteArray);
  82. /** Create new Json value from FJsonValue (to be used from USIOJsonObject) */
  83. static USIOJsonValue* ConstructJsonValue(UObject* WorldContextObject, const TSharedPtr<FJsonValue>& InValue);
  84. /** Create new Json value from JSON encoded string*/
  85. UFUNCTION(BlueprintPure, meta = (DisplayName = "Value From Json String", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "SIOJ|Json")
  86. static USIOJsonValue* ValueFromJsonString(UObject* WorldContextObject, const FString& StringValue);
  87. /** Get the root Json value */
  88. TSharedPtr<FJsonValue>& GetRootValue();
  89. /** Set the root Json value */
  90. void SetRootValue(TSharedPtr<FJsonValue>& JsonValue);
  91. //////////////////////////////////////////////////////////////////////////
  92. // FJsonValue API
  93. /** Get type of Json value (Enum) */
  94. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  95. ESIOJson::Type GetType() const;
  96. /** Get type of Json value (String) */
  97. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  98. FString GetTypeString() const;
  99. /** Returns true if this value is a 'null' */
  100. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  101. bool IsNull() const;
  102. /** Returns this value as a double, throwing an error if this is not an Json Number
  103. * Attn.!! float used instead of double to make the function blueprintable! */
  104. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  105. float AsNumber() const;
  106. /** Returns this value as a string, throwing an error if this is not an Json String */
  107. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  108. FString AsString() const;
  109. /** Returns this value as a boolean, throwing an error if this is not an Json Bool */
  110. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  111. bool AsBool() const;
  112. /** Returns this value as an array, throwing an error if this is not an Json Array */
  113. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  114. TArray<USIOJsonValue*> AsArray() const;
  115. /** Returns this value as an object, throwing an error if this is not an Json Object */
  116. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  117. USIOJsonObject* AsObject();
  118. //Convert message to binary data
  119. UFUNCTION(BlueprintPure, Category = "SIOJ|Json")
  120. TArray<uint8> AsBinary();
  121. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  122. FString EncodeJson() const;
  123. //////////////////////////////////////////////////////////////////////////
  124. // Data
  125. private:
  126. /** Internal JSON data */
  127. TSharedPtr<FJsonValue> JsonVal;
  128. //////////////////////////////////////////////////////////////////////////
  129. // Helpers
  130. protected:
  131. /** Simple error logger */
  132. void ErrorMessage(const FString& InType) const;
  133. };