Table of Contents

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

Func<bool, bool, bool, int, string[], JsonSerializerOptions>

DefaultDecimals

Controls the decimal places when serializing decimal and double values.

public static int DefaultDecimals { get; set; }

Property Value

int

DefaultSerializerOptions

Returns the default serialization options

public static JsonSerializerOptions DefaultSerializerOptions { get; }

Property Value

JsonSerializerOptions

SerializerOptions

Used when no options or null options are specified.

public static JsonSerializerOptions SerializerOptions { get; set; }

Property Value

JsonSerializerOptions

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

Source T
Dest T2

Type Parameters

T
T2

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

Source T

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

CameCase bool
Formatted bool
CaseInsensitiveProperties bool
Decimals int
ExcludeProperties string[]

Returns

JsonSerializerOptions

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

JsonText string
ReturnType Type
JsonOptions JsonSerializerOptions

Returns

object

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

Instance object
Stream Stream
JsonOptions JsonSerializerOptions

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

ClassType Type
Stream Stream
JsonOptions JsonSerializerOptions

Returns

object

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

JsonText string
JsonOptions JsonSerializerOptions

Returns

T

Type Parameters

T

Format(string)

Returns a specified json text as formatted for readability.

public static string Format(string JsonText)

Parameters

JsonText string

Returns

string

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

RequestBodyStream Stream

Returns

Dictionary<string, object>

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

JsonText string
Stream Stream

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

Instance object
FilePath string
Encoding string

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

ClassType Type
FilePath string
Encoding string

Returns

object

ObjectToJsonNode(object)

Converts an object to JsonNode

public static JsonNode ObjectToJsonNode(object Instance)

Parameters

Instance object

Returns

JsonNode

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

Instance object
JsonText string
JsonOptions JsonSerializerOptions

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

Instance object
FilePath string
Encoding string

Serialize(object)

Serializes a specified instance.

public static string Serialize(object Instance)

Parameters

Instance object

Returns

string

Serialize(object, bool, bool)

Serializes a specified instance.

public static string Serialize(object Instance, bool Formatted, bool CameCase = false)

Parameters

Instance object
Formatted bool
CameCase bool

Returns

string

Serialize(object, string[])

Serializes a specified instance.

public static string Serialize(object Instance, string[] ExcludeProperties)

Parameters

Instance object
ExcludeProperties string[]

Returns

string

Serialize(object, JsonSerializerOptions)

Serializes a specified instance.

public static string Serialize(object Instance, JsonSerializerOptions JsonOptions)

Parameters

Instance object
JsonOptions JsonSerializerOptions

Returns

string

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

Instance object
Stream Stream
JsonOptions JsonSerializerOptions

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

Instance object
JsonOptions JsonSerializerOptions

Returns

MemoryStream

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

JsonOptions JsonSerializerOptions
CameCase bool
Formatted bool
CaseInsensitiveProperties bool
Decimals int
ExcludeProperties string[]

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

Stream Stream

Returns

string

ToDictionary(string)

Converts a json text to a Dictionary instance.

public static Dictionary<string, string> ToDictionary(string JsonText)

Parameters

JsonText string

Returns

Dictionary<string, string>

ToDynamic(string)

Converts json text to a dynamic object which actually is a JsonObject

public static dynamic ToDynamic(string JsonText)

Parameters

JsonText string

Returns

dynamic