DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
JsonSerializer.h
Go to the documentation of this file.
1
5
6#pragma once
7
8#include "Common.h"
9#include "System/String.h"
13#include <type_traits>
14
15namespace DotNetDupe {
16 namespace System {
17 namespace Text {
18 namespace Json {
19
23 template <typename T, typename Enable = void>
25
32 public:
37 template <typename T>
38 static String Serialize(const T& value) {
39 JsonElement element = JsonConverter<T>::Write(value);
40 return element.ToString();
41 }
42
47 template <typename T>
48 static T Deserialize(const String& sJson) {
49 JsonElement element = JsonElement::Parse(sJson);
50 return JsonConverter<T>::Read(element);
51 }
52 };
53
54 // Specializations for Primitive types
55
57 template <>
58 struct JsonConverter<int> {
59 static JsonElement Write(const int& value) {
60 return JsonElement(static_cast<double>(value));
61 }
62 static int Read(const JsonElement& element) {
63 return element.GetInt32();
64 }
65 };
66
68 template <>
69 struct JsonConverter<long long> {
70 static JsonElement Write(const long long& value) {
71 return JsonElement(static_cast<double>(value));
72 }
73 static long long Read(const JsonElement& element) {
74 return element.GetInt64();
75 }
76 };
77
79 template <>
80 struct JsonConverter<double> {
81 static JsonElement Write(const double& value) {
82 return JsonElement(value);
83 }
84 static double Read(const JsonElement& element) {
85 return element.GetDouble();
86 }
87 };
88
90 template <>
91 struct JsonConverter<float> {
92 static JsonElement Write(const float& value) {
93 return JsonElement(static_cast<double>(value));
94 }
95 static float Read(const JsonElement& element) {
96 return static_cast<float>(element.GetDouble());
97 }
98 };
99
101 template <>
102 struct JsonConverter<bool> {
103 static JsonElement Write(const bool& value) {
104 return JsonElement(value);
105 }
106 static bool Read(const JsonElement& element) {
107 return element.GetBoolean();
108 }
109 };
110
112 template <>
114 static JsonElement Write(const String& value) {
115 return JsonElement(value);
116 }
117 static String Read(const JsonElement& element) {
118 return element.GetString();
119 }
120 };
121
123 template <typename U>
124 struct JsonConverter<Collections::Generic::List<U>> {
127 for (int i = 0; i < value.GetCount(); ++i) {
129 }
130 return arr;
131 }
134 int iLen = element.GetArrayLength();
135 for (int i = 0; i < iLen; ++i) {
137 }
138 return lst;
139 }
140 };
141
143 template <typename U>
144 struct JsonConverter<Collections::Generic::Dictionary<String, U>> {
147 auto keys = value.GetKeys();
148 for (int i = 0; i < keys.GetLength(); ++i) {
149 obj.SetProperty(keys[i], JsonConverter<U>::Write(value[keys[i]]));
150 }
151 return obj;
152 }
155 auto keys = element.GetPropertyNames();
156 for (int i = 0; i < keys.GetLength(); ++i) {
157 JsonElement prop;
158 if (element.TryGetProperty(keys[i], prop)) {
159 dict.Add(keys[i], JsonConverter<U>::Read(prop));
160 }
161 }
162 return dict;
163 }
164 };
165
166 }
167 }
168 }
169}
Defines common cross-platform macros, export decorators, and fundamental types.
Represents a specific JSON value within a JsonDocument.
Represents a strongly typed list of objects that can be accessed by index mirroring ....
High-performance UTF-8 / UTF-16 string manipulation class mirroring .NET System.String.
Represents a collection of keys and values.
Definition Dictionary.h:66
void Add(const TKey &key, const TValue &value)
Definition Dictionary.h:244
Represents a strongly typed list of objects accessible by index.
Definition List.h:29
int GetCount() const
Gets the number of elements contained in the List.
Definition List.h:100
void Add(const T &item)
Adds an object to the end of the List.
Definition List.h:138
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition String.h:74
Represents a specific JSON value within a JsonDocument or object hierarchy.
Definition JsonElement.h:39
String GetString() const
Gets the value of the element as a string.
int GetArrayLength() const
Gets the number of values contained within the current JSON array value.
int GetInt32() const
Gets the current JSON number as a 32-bit signed integer.
Array< String > GetPropertyNames() const
Gets an array containing the names of all properties on the current JSON object.
bool TryGetProperty(const String &sPropertyName, JsonElement &objValue) const
Looks for a property named propertyName in the current JSON object.
String ToString() const
Serializes the element into a JSON string representation.
JsonElement GetArrayElement(int iIndex) const
Gets the value at the specified index in the current JSON array.
JsonElement()
Initializes a new instance of the JsonElement class representing undefined.
void AddArrayElement(const JsonElement &objElement)
Adds an element to the current JSON array.
long long GetInt64() const
Gets the current JSON number as a 64-bit signed integer.
bool GetBoolean() const
Gets the value of the element as a Boolean.
static JsonElement Parse(const String &sJson)
Parses text representing a single JSON value into a JsonElement.
void SetProperty(const String &sPropertyName, const JsonElement &objValue)
Sets or replaces a property on the current JSON object.
double GetDouble() const
Gets the current JSON number as a double.
Provides functionality to serialize objects to JSON strings and deserialize JSON strings to objects.
static T Deserialize(const String &sJson)
Parses the text representing a single JSON value into an instance of the type specified by a generic ...
static String Serialize(const T &value)
Converts the value of a type specified by a generic type parameter into a JSON string.
static Collections::Generic::Dictionary< String, U > Read(const JsonElement &element)
static JsonElement Write(const Collections::Generic::Dictionary< String, U > &value)
static JsonElement Write(const Collections::Generic::List< U > &value)
static Collections::Generic::List< U > Read(const JsonElement &element)
static JsonElement Write(const String &value)
static String Read(const JsonElement &element)
static bool Read(const JsonElement &element)
static JsonElement Write(const bool &value)
static double Read(const JsonElement &element)
static JsonElement Write(const double &value)
static float Read(const JsonElement &element)
static JsonElement Write(const float &value)
static JsonElement Write(const int &value)
static int Read(const JsonElement &element)
static long long Read(const JsonElement &element)
static JsonElement Write(const long long &value)
Primary template for type-specific JSON serialization converters.