SIOJsonObject.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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/JsonObject.h"
  6. #include "SIOJsonObject.generated.h"
  7. class USIOJsonValue;
  8. /**
  9. * Blueprintable FJsonObject wrapper
  10. */
  11. UCLASS(BlueprintType, Blueprintable)
  12. class SIOJSON_API USIOJsonObject : public UObject
  13. {
  14. GENERATED_UCLASS_BODY()
  15. /** Create new Json object, cannot be pure */
  16. UFUNCTION(BlueprintCallable , meta = (DisplayName = "Construct Json Object", KeyWords = "create make", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"), Category = "SIOJ|Json")
  17. static USIOJsonObject* ConstructJsonObject(UObject* WorldContextObject);
  18. /** Reset all internal data */
  19. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  20. void Reset();
  21. /** Get the root Json object */
  22. TSharedPtr<FJsonObject>& GetRootObject();
  23. /** Set the root Json object */
  24. void SetRootObject(const TSharedPtr<FJsonObject>& JsonObject);
  25. //////////////////////////////////////////////////////////////////////////
  26. // Serialization
  27. /** Serialize Json to string (formatted with line breaks) */
  28. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  29. FString EncodeJson() const;
  30. /** Serialize Json to string (single string without line breaks) */
  31. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  32. FString EncodeJsonToSingleString() const;
  33. /** Construct Json object from string */
  34. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  35. bool DecodeJson(const FString& JsonString);
  36. //////////////////////////////////////////////////////////////////////////
  37. // FJsonObject API
  38. /** Returns a list of field names that exist in the object */
  39. UFUNCTION(BlueprintPure, Category = "SIOJ|Json")
  40. TArray<FString> GetFieldNames();
  41. /** Checks to see if the FieldName exists in the object */
  42. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  43. bool HasField(const FString& FieldName) const;
  44. /** Remove field named FieldName */
  45. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  46. void RemoveField(const FString& FieldName);
  47. /** Get the field named FieldName as a JsonValue */
  48. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  49. USIOJsonValue* GetField(const FString& FieldName) const;
  50. /** Add a field named FieldName with a Value */
  51. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  52. void SetField(const FString& FieldName, USIOJsonValue* JsonValue);
  53. /** Get the field named FieldName as a Json Array */
  54. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  55. TArray<USIOJsonValue*> GetArrayField(const FString& FieldName);
  56. /** Set an ObjectField named FieldName and value of Json Array */
  57. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  58. void SetArrayField(const FString& FieldName, const TArray<USIOJsonValue*>& InArray);
  59. /** Adds all of the fields from one json object to this one */
  60. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  61. void MergeJsonObject(USIOJsonObject* InJsonObject, bool Overwrite);
  62. //////////////////////////////////////////////////////////////////////////
  63. // FJsonObject API Helpers (easy to use with simple Json objects)
  64. /** Get the field named FieldName as a number. Ensures that the field is present and is of type Json number.
  65. * Attn.!! float used instead of double to make the function blueprintable! */
  66. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  67. float GetNumberField(const FString& FieldName) const;
  68. /** Add a field named FieldName with Number as value
  69. * Attn.!! float used instead of double to make the function blueprintable! */
  70. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  71. void SetNumberField(const FString& FieldName, float Number);
  72. /** Get the field named FieldName as a string. */
  73. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  74. FString GetStringField(const FString& FieldName) const;
  75. /** Add a field named FieldName with value of StringValue */
  76. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  77. void SetStringField(const FString& FieldName, const FString& StringValue);
  78. /** Get the field named FieldName as a boolean. */
  79. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  80. bool GetBoolField(const FString& FieldName) const;
  81. /** Set a boolean field named FieldName and value of InValue */
  82. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  83. void SetBoolField(const FString& FieldName, bool InValue);
  84. /** Get the field named FieldName as a Json object. */
  85. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  86. USIOJsonObject* GetObjectField(const FString& FieldName) const;
  87. /** Set an ObjectField named FieldName and value of JsonObject */
  88. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  89. void SetObjectField(const FString& FieldName, USIOJsonObject* JsonObject);
  90. /** Get the field named FieldName as a binary buffer array. */
  91. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  92. void GetBinaryField(const FString& FieldName, TArray<uint8>& OutBinary) const;
  93. /** Set an BinaryField named FieldName and binary buffer array */
  94. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  95. void SetBinaryField(const FString& FieldName, const TArray<uint8>& Bytes);
  96. //////////////////////////////////////////////////////////////////////////
  97. // Array fields helpers (uniform arrays)
  98. /** Get the field named FieldName as a Number Array. Use it only if you're sure that array is uniform!
  99. * Attn.!! float used instead of double to make the function blueprintable! */
  100. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  101. TArray<float> GetNumberArrayField(const FString& FieldName);
  102. /** Set an ObjectField named FieldName and value of Number Array
  103. * Attn.!! float used instead of double to make the function blueprintable! */
  104. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  105. void SetNumberArrayField(const FString& FieldName, const TArray<float>& NumberArray);
  106. /** Get the field named FieldName as a String Array. Use it only if you're sure that array is uniform! */
  107. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  108. TArray<FString> GetStringArrayField(const FString& FieldName);
  109. /** Set an ObjectField named FieldName and value of String Array */
  110. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  111. void SetStringArrayField(const FString& FieldName, const TArray<FString>& StringArray);
  112. /** Get the field named FieldName as a Bool Array. Use it only if you're sure that array is uniform! */
  113. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  114. TArray<bool> GetBoolArrayField(const FString& FieldName);
  115. /** Set an ObjectField named FieldName and value of Bool Array */
  116. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  117. void SetBoolArrayField(const FString& FieldName, const TArray<bool>& BoolArray);
  118. /** Get the field named FieldName as an Object Array. Use it only if you're sure that array is uniform! */
  119. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  120. TArray<USIOJsonObject*> GetObjectArrayField(const FString& FieldName);
  121. /** Set an ObjectField named FieldName and value of Ob Array */
  122. UFUNCTION(BlueprintCallable, Category = "SIOJ|Json")
  123. void SetObjectArrayField(const FString& FieldName, const TArray<USIOJsonObject*>& ObjectArray);
  124. //////////////////////////////////////////////////////////////////////////
  125. // Data
  126. private:
  127. /** Internal JSON data */
  128. TSharedPtr<FJsonObject> JsonObj;
  129. };