Class Json
A very simple, light-weight JSON library written by Mario to teach Geo how things are done
This is an useful helper for small tasks but it doesn't represent a full-featured serializer such as the beloved Json.NET.
Inherited Members
Namespace: Swan.Formatters
Syntax
public static class Json
Methods
Deserialize(String)
Deserializes the specified json string as either a Dictionary[string, object] or as a List[object] depending on the syntax of the JSON string.
Declaration
public static object Deserialize(string json)
Parameters
Type | Name | Description |
---|---|---|
String | json | The JSON string. |
Returns
Type | Description |
---|---|
Object | Type of the current deserializes. |
Examples
The following code shows how to deserialize a JSON string into a Dictionary.
using Swan.Formatters;
class Example
{
static void Main()
{
// json to deserialize
var basicJson = "{\"One\":\"One\",\"Two\":\"Two\",\"Three\":\"Three\"}";
// deserializes the specified json into a Dictionary<string, object>.
var data = Json.Deserialize(basicJson);
}
}
Deserialize(String, JsonSerializerCase)
Deserializes the specified json string as either a Dictionary[string, object] or as a List[object] depending on the syntax of the JSON string.
Declaration
public static object Deserialize(string json, JsonSerializerCase jsonSerializerCase)
Parameters
Type | Name | Description |
---|---|---|
String | json | The JSON string. |
JsonSerializerCase | jsonSerializerCase | The json serializer case. |
Returns
Type | Description |
---|---|
Object | Type of the current deserializes. |
Examples
The following code shows how to deserialize a JSON string into a Dictionary.
using Swan.Formatters;
class Example
{
static void Main()
{
// json to deserialize
var basicJson = "{\"One\":\"One\",\"Two\":\"Two\",\"Three\":\"Three\"}";
// deserializes the specified json into a Dictionary<string, object>.
var data = Json.Deserialize(basicJson, JsonSerializerCase.None);
}
}
Deserialize(String, Type, Boolean, JsonSerializerCase)
Deserializes the specified JSON string and converts it to the specified object type.
Declaration
public static object Deserialize(string json, Type resultType, bool includeNonPublic = false, JsonSerializerCase jsonSerializerCase = JsonSerializerCase.None)
Parameters
Type | Name | Description |
---|---|---|
String | json | The JSON string. |
Type | resultType | Type of the result. |
Boolean | includeNonPublic | if set to true, it also uses the non-public constructors and property setters. |
JsonSerializerCase | jsonSerializerCase | The json serializer case. |
Returns
Type | Description |
---|---|
Object | Type of the current conversion from json result. |
Deserialize<T>(String, JsonSerializerCase)
Deserializes the specified JSON string and converts it to the specified object type. Non-public constructors and property setters are ignored.
Declaration
public static T Deserialize<T>(string json, JsonSerializerCase jsonSerializerCase = JsonSerializerCase.None)
Parameters
Type | Name | Description |
---|---|---|
String | json | The JSON string. |
JsonSerializerCase | jsonSerializerCase | The JSON serializer case. |
Returns
Type | Description |
---|---|
T | The deserialized specified type object. |
Type Parameters
Name | Description |
---|---|
T | The type of object to deserialize. |
Examples
The following code describes how to deserialize a JSON string into an object of type T.
using Swan.Formatters;
class Example
{
static void Main()
{
// json type BasicJson to serialize
var basicJson = "{\"One\":\"One\",\"Two\":\"Two\",\"Three\":\"Three\"}";
// deserializes the specified string in a new instance of the type BasicJson.
var data = Json.Deserialize<BasicJson>(basicJson);
}
}
Deserialize<T>(String, Boolean)
Deserializes the specified JSON string and converts it to the specified object type.
Declaration
public static T Deserialize<T>(string json, bool includeNonPublic)
Parameters
Type | Name | Description |
---|---|---|
String | json | The JSON string. |
Boolean | includeNonPublic | if set to true, it also uses the non-public constructors and property setters. |
Returns
Type | Description |
---|---|
T | The deserialized specified type object. |
Type Parameters
Name | Description |
---|---|
T | The type of object to deserialize. |
Serialize(Object, JsonSerializerCase, Boolean, String)
Serializes the specified object into a JSON string.
Declaration
public static string Serialize(object obj, JsonSerializerCase jsonSerializerCase, bool format = false, string typeSpecifier = null)
Parameters
Type | Name | Description |
---|---|---|
Object | obj | The object. |
JsonSerializerCase | jsonSerializerCase | The json serializer case. |
Boolean | format | if set to |
String | typeSpecifier | The type specifier. |
Returns
Type | Description |
---|---|
String | A String that represents the current object. |
Serialize(Object, SerializerOptions)
Serializes the specified object using the SerializerOptions provided.
Declaration
public static string Serialize(object obj, SerializerOptions options)
Parameters
Type | Name | Description |
---|---|---|
Object | obj | The object. |
SerializerOptions | options | The options. |
Returns
Type | Description |
---|---|
String | A String that represents the current object. |
Serialize(Object, Boolean, String, Boolean, String[], String[])
Serializes the specified object into a JSON string.
Declaration
public static string Serialize(object obj, bool format = false, string typeSpecifier = null, bool includeNonPublic = false, string[] includedNames = null, params string[] excludedNames)
Parameters
Type | Name | Description |
---|---|---|
Object | obj | The object. |
Boolean | format | if set to |
String | typeSpecifier | The type specifier. Leave null or empty to avoid setting. |
Boolean | includeNonPublic | if set to |
String[] | includedNames | The included property names. |
String[] | excludedNames | The excluded property names. |
Returns
Type | Description |
---|---|
String | A String that represents the current object. |
Examples
The following example describes how to serialize a simple object.
using Swan.Formatters;
class Example
{
static void Main()
{
var obj = new { One = "One", Two = "Two" };
var serial = Json.Serialize(obj); // {"One": "One","Two": "Two"}
}
}
The following example details how to serialize an object using the JsonPropertyAttribute.
using Swan.Attributes;
using Swan.Formatters;
class Example
{
class JsonPropertyExample
{
[JsonProperty("data")]
public string Data { get; set; }
[JsonProperty("ignoredData", true)]
public string IgnoredData { get; set; }
}
static void Main()
{
var obj = new JsonPropertyExample() { Data = "OK", IgnoredData = "OK" };
// {"data": "OK"}
var serializedObj = Json.Serialize(obj);
}
}
Serialize(Object, Boolean, String, Boolean, String[], String[], Nullable<List<WeakReference>>, JsonSerializerCase)
Serializes the specified object into a JSON string.
Declaration
public static string Serialize(object obj, bool format, string typeSpecifier, bool includeNonPublic, string[] includedNames, string[] excludedNames, List<WeakReference>? parentReferences, JsonSerializerCase jsonSerializerCase)
Parameters
Type | Name | Description |
---|---|---|
Object | obj | The object. |
Boolean | format | if set to |
String | typeSpecifier | The type specifier. Leave null or empty to avoid setting. |
Boolean | includeNonPublic | if set to |
String[] | includedNames | The included property names. |
String[] | excludedNames | The excluded property names. |
Nullable<List<WeakReference>> | parentReferences | The parent references. |
JsonSerializerCase | jsonSerializerCase | The json serializer case. |
Returns
Type | Description |
---|---|
String | A String that represents the current object. |
SerializeExcluding(Object, Boolean, String[])
Serializes the specified object excluding the specified property names.
Declaration
public static string SerializeExcluding(object obj, bool format, params string[] excludeNames)
Parameters
Type | Name | Description |
---|---|---|
Object | obj | The object. |
Boolean | format | if set to |
String[] | excludeNames | The exclude names. |
Returns
Type | Description |
---|---|
String | A String that represents the current object. |
Examples
The following code shows how to serialize a simple object excluding the specified properties.
using Swan.Formatters;
class Example
{
static void Main()
{
// object to serialize
var obj = new { One = "One", Two = "Two", Three = "Three" };
// the excluded names
var excludeNames = new[] { "Two", "Three" };
// serialize excluding
var data = Json.SerializeExcluding(basicObject, false, includedNames);
// {"One": "One"}
}
}
SerializeOnly(Object, Boolean, String[])
Serializes the specified object only including the specified property names.
Declaration
public static string SerializeOnly(object obj, bool format, params string[] includeNames)
Parameters
Type | Name | Description |
---|---|---|
Object | obj | The object. |
Boolean | format | if set to |
String[] | includeNames | The include names. |
Returns
Type | Description |
---|---|
String | A String that represents the current object. |
Examples
The following example shows how to serialize a simple object including the specified properties.
using Swan.Formatters;
class Example
{
static void Main()
{
// object to serialize
var obj = new { One = "One", Two = "Two", Three = "Three" };
// the included names
var includedNames = new[] { "Two", "Three" };
// serialize only the included names
var data = Json.SerializeOnly(basicObject, true, includedNames);
// {"Two": "Two","Three": "Three" }
}
}