Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/halak/jvalue
super lightweight C# json parser
https://github.com/halak/jvalue
c-sharp json json-parser lightweight
Last synced: 3 months ago
JSON representation
super lightweight C# json parser
- Host: GitHub
- URL: https://github.com/halak/jvalue
- Owner: halak
- License: mit
- Created: 2013-12-15T13:41:12.000Z (almost 11 years ago)
- Default Branch: main
- Last Pushed: 2023-12-02T13:45:49.000Z (12 months ago)
- Last Synced: 2024-07-01T13:52:48.186Z (5 months ago)
- Topics: c-sharp, json, json-parser, lightweight
- Language: C#
- Homepage:
- Size: 680 KB
- Stars: 22
- Watchers: 4
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
JValue
======
Lightweight C# Json Parser.
JValue is simple and pure.Features
--------
- Pros
- Small memory footprint. (no heap allocation)
- Fastest parse.
- Easy to install.
- Easy to use.
- Unity3D friendly.
- Multithread safe. (JValue is immutable type)
- Cons
- When you get to repeat the same key, it is inefficient.
- Object mapping NOT supported. (`System.Reflection`)Simple usage
------------
```cs
JValue location = JValue.Parse(@"{""x"":10, ""y"":20}");
Console.WriteLine((int)location["x"]); // 10
Console.WriteLine((int)location["y"]); // 20
Console.WriteLine(location["z"].ToString()); // nullvar person = new Dictionary()
{
{"name", "John"},
{"age", 27},
};
Console.WriteLine(new JValue(person).ToString()); // {"name":"John","age":27}
```Mechanism
---------
1. `JValue` is `struct`.
2. `JValue` has three members. `{string source, int startIndex, int length}````cs
// raw source
// {"name":"John", "age":27, "friend":{"name":"Tom"}}
JValue person = JValue.Parse(...);
// person.source = ...
// person.startIndex = 0
// person.length = 50JValue name = person["name"];
// name.source = person.source
// name.startIndex = 8
// name.length = 6JValue age = person["age"];
// age.source = person.source
// age.startIndex = 22
// age.length = 2JValue friend = person["friend"];
// friend.source = person.source
// friend.startIndex = 35
// friend.length = 14JValue friendName = friend["name"];
// friendName.source = friend.source = person.source
// friendName.startIndex = 43
// friendName.length = 5string nameValue = (string)name; // At this time, encode string.
int ageValue = (int)age; // At this time, parse int.
```Usage
-----
```cs
JValue book = JValue.Parse(@"{
""name"": ""Json guide"",
""pages"": 400,
""tags"": [""computer"", ""data-interchange format"", ""easy""],
""price"": {""usd"":0.99, ""krw"":1000}
}");
string name = book["name"];
Console.WriteLine("Name: {0}", name);int pages = book["pages"];
Console.WriteLine("Pages: {0}", pages);Console.WriteLine("Primary tag: {0}", book["tags"][0].AsString());
Console.WriteLine("Tags:");
foreach (var item in book["tags"].Array())
Console.WriteLine("\t{0}", item);
Console.WriteLine("Unknown tag: {0}", book["tags"][100].AsString());JValue price = book["price"];
Console.WriteLine("Price: ${0}", (double)price["usd"]);
Console.WriteLine("");
Console.WriteLine("Reserialization");
Console.WriteLine(book.Serialize(false));/*
Name: Json guide
Pages: 400
Primary tag: computer
Tags:
"computer"
"data-interchange format"
"easy"
Unknown author:
Price: $0.99Reserialization
{"name":"Json guide","pages":400,"tags":["computer","data-interchange format","easy"],"price":{"usd":0.99,"krw":1000}}
*/
```