项目作者: yutopp

项目描述 :
A JSON serializer/deserializer (with JsonSchema support) library written in pure C#
高级语言: C#
项目地址: git://github.com/yutopp/VJson.git
创建时间: 2019-02-05T17:16:01Z
项目社区:https://github.com/yutopp/VJson

开源协议:Boost Software License 1.0

下载


VJson 🍣

A JSON serializer/deserializer (with JsonSchema support) library written in pure C#.

ci
npm
NuGet Badge
codecov
license

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.

Installation

For standard C# projects

You can use Nuget/VJson.

  1. dotnet add package VJson

For Unity projects

stable

Add scoped registry information shown below to your Packages/manifest.json if not exists.

  1. {
  2. "scopedRegistries": [
  3. {
  4. "name": "yutopp.net",
  5. "url": "https://registry.npmjs.com",
  6. "scopes": [
  7. "net.yutopp"
  8. ]
  9. }
  10. ]
  11. }

And add net.yutopp.vjson to your Packages/manifest.json like below.

  1. {
  2. "dependencies": {
  3. "net.yutopp.vjson": "*"
  4. }
  5. }

nightly

Add a url for VJson git repository to your Packages/manifest.json like below.

  1. {
  2. "dependencies": {
  3. "net.yutopp.vjson": "https://github.com/yutopp/VJson.git?path=Packages/net.yutopp.vjson"
  4. }
  5. }

(TODO: Provide unity packages)

Usage example

Serialize/Deserialize

  1. //
  2. // For serialization
  3. //
  4. var serializer = new VJson.JsonSerializer(typeof(int));
  5. // You can get JSON strings,
  6. var json = serializer.Serialize(42);
  7. // OR write json data(UTF-8) to streams directly.
  8. using (var s = new MemoryStream())
  9. {
  10. serializer.Serialize(s, 42);
  11. }
  1. //
  2. // For deserialization
  3. //
  4. var serializer = new VJson.JsonSerializer(typeof(int));
  5. // You can get Object from strings,
  6. var value = serializer.Deserialize(json);
  7. // OR read json data(UTF-8) from streams directly.
  8. using (var s = new MemoryStream(Encoding.UTF8.GetBytes(json)))
  9. {
  10. var value = serializer.Deserialize(s);
  11. }

VJson supports serializing/deserializing of some System.Collections(.Generics) classes listed below,

  • List
  • Dictionary
  • 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.)

  1. class SomeObject
  2. {
  3. private float _p = 3.14f; // Fields which are non-public will not be exported by default.
  4. [JsonField] long _p2 = 4; // Fields which are non-public BUT having [JsonField] (+etc) attributes will BE exported!
  5. public int X; // Fields which are public will be exported by default, but we strongly recommended to add [JsonField] attributes like below.
  6. [JsonField] public string Y;
  7. }
  8. var obj = new SomeObject {
  9. X = 10,
  10. Y = "abab",
  11. },
  12. var serializer = new VJson.JsonSerializer(typeof(SomeObject));
  13. var json = serializer.Serialize(obj);
  14. // > {"_p2": 4,"X":10,"Y":"abab"}
  15. var v = serializer.Deserialize("{\"X\":10,\"Y\":\"abab\"}");
  16. // > v becomes same as obj.

Attributes

JSON Schema and validation

VJson supports JSON Schema draft7.

(TODO: Write examples)

Attributes

(TODO: Write examples)

Other use cases are available at here.

Tasks

  • Performance tuning

License

Boost Software License - Version 1.0

References

Author