A minimalistic JSON handler library.
Json.Net (.NET Standard 2.0) : https://www.nuget.org/packages/Json.Net/
Json.Net.Core (.NET 5.0) : https://www.nuget.org/packages/Json.Net.Core/
Define a POCO class...
class Pet
{
public int id;
public string name;
}
Serialization...
var petJson = JsonNet.Serialize(pet);
Deserialization...
var pet = JsonNet.Deserialize<Pet>(petJson);
You can also define and use custom type converters to control serialization/deserialization.
var dateConverter =
new JsonConverter<DateTime>(
dt => dt.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss", CultureInfo.InvariantCulture),
s => DateTime.ParseExact(s, "yyyy'-'MM'-'dd'T'HH':'mm':'ss", CultureInfo.InvariantCulture));
var petJson = JsonNet.Serialize(pet, dateConverter);
using Json.Net;
string JsonNet.Serialize(object obj, params IJsonConverter[] converters)
Serializes an object to its JSON text representation.
obj : Object to be serialized
converters : Custom type converters. Default: empty
void Serialize(object obj, Stream stream, params IJsonConverter[] converters)
Serializes an object to a JSON text stream destination.
obj : Object to be serialized
stream : JSON stream
converters : Custom type converters. Default: empty
void Serialize(object obj, TextWriter writer, params IJsonConverter[] converters)
Serializes an object to a JSON text writer destination.
obj : Object to be serialized
writer : JSON text writer
converters : Custom type converters. Default: empty
T JsonNet.Deserialize<T>(string json, params IJsonConverter[] converters)
Deserializes an object from a JSON text.
T : Deserialized object's type
json : JSON text
converters : Custom converters. Default: empty
T Deserialize<T>(Stream stream, params IJsonConverter[] converters)
Deserializes an object from a JSON text stream source.
T : Deserialized object's type
stream : JSON stream
converters : Custom converters. Default: empty
T Deserialize<T>(TextReader reader, params IJsonConverter[] converters)
Deserializes an object from a JSON text reader source.
T : Deserialized object's type
reader : JSON text reader
converters : Custom converters. Default: empty
public interface IJsonConverter
{
Type GetConvertingType();
string Serializer(object obj);
object Deserializer(string txt);
}