A JSON serializer/deserializer (with JsonSchema support) library written in pure C#.
VJson
is a JSON serializer/deserializer (with JsonSchema support) library written in pure C#. Supported versions are .NET Standard 2.0
or higher.
This library is developed as a purely C# project, however it also supports that be build with Unity 2019.4.22f1
or higher.
You can use Nuget/VJson.
dotnet add package VJson
Add scoped registry information shown below to your Packages/manifest.json
if not exists.
{
"scopedRegistries": [
{
"name": "yutopp.net",
"url": "https://registry.npmjs.com",
"scopes": [
"net.yutopp"
]
}
]
}
And add net.yutopp.vjson
to your Packages/manifest.json
like below.
{
"dependencies": {
"net.yutopp.vjson": "*"
}
}
Add a url for VJson git repository to your Packages/manifest.json
like below.
{
"dependencies": {
"net.yutopp.vjson": "https://github.com/yutopp/VJson.git?path=Packages/net.yutopp.vjson"
}
}
(TODO: Provide unity packages)
//
// For serialization
//
var serializer = new VJson.JsonSerializer(typeof(int));
// You can get JSON strings,
var json = serializer.Serialize(42);
// OR write json data(UTF-8) to streams directly.
using (var s = new MemoryStream())
{
serializer.Serialize(s, 42);
}
//
// For deserialization
//
var serializer = new VJson.JsonSerializer(typeof(int));
// You can get Object from strings,
var value = serializer.Deserialize(json);
// OR read json data(UTF-8) from streams directly.
using (var s = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
var value = serializer.Deserialize(s);
}
VJson
supports serializing/deserializing of some System.Collections(.Generics)
classes listed below,
- List
- Dictionary<string, T>
- Array
and user defined classes. For user defined classes, converting only all public fields are supported.
e.g.
(It is strongly recommended to always add VJson attributes such as [JsonField] to fields that you want to treat as Json. This will avoid problems with IL2CPP, especially when using Unity.)
class SomeObject
{
private float _p = 3.14f; // Fields which are non-public will not be exported by default.
[JsonField] long _p2 = 4; // Fields which are non-public BUT having [JsonField] (+etc) attributes will BE exported!
public int X; // Fields which are public will be exported by default, but we strongly recommended to add [JsonField] attributes like below.
[JsonField] public string Y;
}
var obj = new SomeObject {
X = 10,
Y = "abab",
},
var serializer = new VJson.JsonSerializer(typeof(SomeObject));
var json = serializer.Serialize(obj);
// > {"_p2": 4,"X":10,"Y":"abab"}
var v = serializer.Deserialize("{\"X\":10,\"Y\":\"abab\"}");
// > v becomes same as obj.
...
VJson
supports JSON Schema draft7.
(TODO: Write examples)
(TODO: Write examples)
Other use cases are available at here.
- Performance tuning