Class Json
- Namespace
- Tripous
- Assembly
- Tripous.dll
Helper json static class
SEE: https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-overview
public static class Json
- Inheritance
-
Json
- Inherited Members
Properties
CreateOptionsFunc
This property is used whenever this class is about to create JsonSerializerOptions.
If this property is null then the CreateJsonOptions(bool, bool, bool, int, string[])() method is used.
The following is the signature the callback should have.
JsonSerializerOptions CreateOptionsFunc(bool CameCase, bool Formatted, bool CaseInsensitiveProperties = true, int Decimals, string[] ExcludeProperties)
public static Func<bool, bool, bool, int, string[], JsonSerializerOptions> CreateOptionsFunc { get; set; }
Property Value
DefaultDecimals
Controls the decimal places when serializing decimal and double values.
public static int DefaultDecimals { get; set; }
Property Value
DefaultSerializerOptions
Returns the default serialization options
public static JsonSerializerOptions DefaultSerializerOptions { get; }
Property Value
SerializerOptions
Used when no options or null options are specified.
public static JsonSerializerOptions SerializerOptions { get; set; }
Property Value
Methods
AssignObject<T, T2>(T, T2)
Assigns a source object properties to a dest object
public static void AssignObject<T, T2>(T Source, T2 Dest)
Parameters
SourceTDestT2
Type Parameters
TT2
CloneObject<T>(T)
Clones an object.
The specified instance should be marked with the SerializableAttribute and provide a default constructor.
public static T CloneObject<T>(T Source) where T : new()
Parameters
SourceT
Returns
- T
Type Parameters
T
CreateJsonOptions(bool, bool, bool, int, string[])
Creates and returns json serialization options
public static JsonSerializerOptions CreateJsonOptions(bool CameCase = false, bool Formatted = true, bool CaseInsensitiveProperties = true, int Decimals = 0, string[] ExcludeProperties = null)
Parameters
Returns
Deserialize(string, Type, JsonSerializerOptions)
Deserializes (creates) an object of a specified type by deserializing a specified json text.
If no options specified then it uses the SerializerOptions options
public static object Deserialize(string JsonText, Type ReturnType, JsonSerializerOptions JsonOptions = null)
Parameters
JsonTextstringReturnTypeTypeJsonOptionsJsonSerializerOptions
Returns
DeserializeFromStream(object, Stream, JsonSerializerOptions)
Loads an object's properties from a specified stream, after reading the json text from the stream.
If no settings specified then it uses the default JsonSerializerSettings
NOTE: UTF8 encoding is used.
public static void DeserializeFromStream(object Instance, Stream Stream, JsonSerializerOptions JsonOptions = null)
Parameters
InstanceobjectStreamStreamJsonOptionsJsonSerializerOptions
DeserializeFromStream(Type, Stream, JsonSerializerOptions)
Reads the json text from a stream and then deserializes (creates) an object of a specified type.
If no settings specified then it uses the default JsonSerializerSettings
NOTE: UTF8 encoding is used.
public static object DeserializeFromStream(Type ClassType, Stream Stream, JsonSerializerOptions JsonOptions = null)
Parameters
ClassTypeTypeStreamStreamJsonOptionsJsonSerializerOptions
Returns
Deserialize<T>(string, JsonSerializerOptions)
Deserializes (creates) an object of a specified type by deserializing a specified json text.
If no options specified then it uses the SerializerOptions options
public static T Deserialize<T>(string JsonText, JsonSerializerOptions JsonOptions = null)
Parameters
JsonTextstringJsonOptionsJsonSerializerOptions
Returns
- T
Type Parameters
T
Format(string)
Returns a specified json text as formatted for readability.
public static string Format(string JsonText)
Parameters
JsonTextstring
Returns
GetRequestDic(Stream)
Returns the text of the input stream of a request (HttpContext.Request.Body) as a Dictionary. To be used when POST-ing json data.
public static Dictionary<string, dynamic> GetRequestDic(Stream RequestBodyStream)
Parameters
RequestBodyStreamStream
Returns
JsonTextToStream(string, Stream)
Converts a specified json text to a stream.
NOTE: UTF8 encoding is used.
public static void JsonTextToStream(string JsonText, Stream Stream)
Parameters
LoadFromFile(object, string, string)
Loads the properties of an instance by reading the json text of a specified file.
public static void LoadFromFile(object Instance, string FilePath, string Encoding = "utf-8")
Parameters
LoadFromFile(Type, string, string)
Creates and returns an object of ClassType using the json text of a specified file
public static object LoadFromFile(Type ClassType, string FilePath, string Encoding = "utf-8")
Parameters
Returns
ObjectToJsonNode(object)
Converts an object to JsonNode
public static JsonNode ObjectToJsonNode(object Instance)
Parameters
Instanceobject
Returns
PopulateObject(object, string, JsonSerializerOptions)
Loads an object's properties from a specified json text.
If no options specified then it uses the SerializerOptions options
public static void PopulateObject(object Instance, string JsonText, JsonSerializerOptions JsonOptions = null)
Parameters
InstanceobjectJsonTextstringJsonOptionsJsonSerializerOptions
SaveToFile(object, string, string)
Saves an instance as json text in a specified file.
public static void SaveToFile(object Instance, string FilePath, string Encoding = "utf-8")
Parameters
Serialize(object)
Serializes a specified instance.
public static string Serialize(object Instance)
Parameters
Instanceobject
Returns
Serialize(object, bool, bool)
Serializes a specified instance.
public static string Serialize(object Instance, bool Formatted, bool CameCase = false)
Parameters
Returns
Serialize(object, string[])
Serializes a specified instance.
public static string Serialize(object Instance, string[] ExcludeProperties)
Parameters
Returns
Serialize(object, JsonSerializerOptions)
Serializes a specified instance.
public static string Serialize(object Instance, JsonSerializerOptions JsonOptions)
Parameters
InstanceobjectJsonOptionsJsonSerializerOptions
Returns
SerializeToStream(object, Stream, JsonSerializerOptions)
Converts Instance to a json string using the NewtonSoft json serializer and then to stream.
If no settings specified then it uses the default JsonSerializerSettings
NOTE: UTF8 encoding is used.
public static void SerializeToStream(object Instance, Stream Stream, JsonSerializerOptions JsonOptions = null)
Parameters
InstanceobjectStreamStreamJsonOptionsJsonSerializerOptions
SerializeToStream(object, JsonSerializerOptions)
Converts Instance to a json string using the NewtonSoft json serializer and then to stream.
If no settings specified then it uses the default JsonSerializerSettings
NOTE: UTF8 encoding is used.
public static MemoryStream SerializeToStream(object Instance, JsonSerializerOptions JsonOptions = null)
Parameters
InstanceobjectJsonOptionsJsonSerializerOptions
Returns
SetupJsonOptions(JsonSerializerOptions, bool, bool, bool, int, string[])
Sets up the specified JsonSerializerOptions instance.
public static void SetupJsonOptions(JsonSerializerOptions JsonOptions, bool CameCase = false, bool Formatted = true, bool CaseInsensitiveProperties = true, int Decimals = 0, string[] ExcludeProperties = null)
Parameters
JsonOptionsJsonSerializerOptionsCameCaseboolFormattedboolCaseInsensitivePropertiesboolDecimalsintExcludePropertiesstring[]
StreamToJsonText(Stream)
Reads a stream as json text.
NOTE: UTF8 encoding is used.
The stream is left open and is not disposed by this method.
public static string StreamToJsonText(Stream Stream)
Parameters
StreamStream
Returns
ToDictionary(string)
Converts a json text to a Dictionary instance.
public static Dictionary<string, string> ToDictionary(string JsonText)
Parameters
JsonTextstring
Returns
ToDynamic(string)
Converts json text to a dynamic object which actually is a JsonObject
public static dynamic ToDynamic(string JsonText)
Parameters
JsonTextstring
Returns
- dynamic