## Description
TinyJSON is a simple JSON library for C# that strives for ease of use.
## Features
* Transmogrify objects into JSON and back again.
* Uses reflection to dump and load object graphs automagically.
* Supports primitives, classes, structs, enums, lists, dictionaries and arrays.
* Supports single dimensional arrays, multidimensional arrays and jagged arrays.
* Parsed data uses proxy variants that can be implicitly cast to primitive types for cleaner code, or directly encoded back to JSON.
* Numeric types are handled without fuss.
* Polymorphic classes supported with a type hint encoded into the JSON.
* Supports optionally pretty printing JSON output.
* Supports optionally encode properties and private fields.
* Supports decoding fields and properties from aliased names.
* Unit tested.
## Usage
The API is namespaced under `TinyJSON` and the primary class is `JSON`. There are really only a few methods you need to know:
```csharp
namespace TinyJSON
{
public static class JSON
{
public static Variant Load( string json );
public static string Dump( object data, EncodeOptions = EncodeOptions.None );
public static void MakeInto<T>( Variant data, out T item );
}
}
```
`Load()` will load a string of JSON, returns `null` if invalid or a `Variant` proxy object if successful. The proxy allows for implicit casts and can convert between various C# numeric value types.
```csharp
var data = JSON.Load( "{\"foo\": 1, \"bar\": 2.34}" );
int i = data["foo"];
float f = data["bar"];
```
`Dump()` will take a C# object, list, dictionary or primitive value type and turn it into JSON.
```csharp
var data = new List<int>() { { 0 }, { 1 }, { 2 } };
Console.WriteLine( JSON.Dump( data ) ); // output: [1,2,3]
```
TinyJSON can also handle classes, structs, enums and nested objects. Given these definitions:
```csharp
enum TestEnum
{
Thing1,
Thing2,
Thing3
}
struct TestStruct
{
public int x;
public int y;
}
class TestClass
{
public string name;
public TestEnum type;
public List<TestStruct> data = new List<TestStruct>();
[Exclude]
public int _ignored;
[BeforeEncode]
public void BeforeEncode()
{
Console.WriteLine( "BeforeEncode callback fired!" );
}
[AfterDecode]
public void AfterDecode()
{
Console.WriteLine( "AfterDecode callback fired!" );
}
}
```
The following code:
```csharp
var testClass = new TestClass();
testClass.name = "Rumpelstiltskin Jones";
testClass.type = TestEnum.Thing2;
testClass.data.Add( new TestStruct() { x = 1, y = 2 } );
testClass.data.Add( new TestStruct() { x = 3, y = 4 } );
testClass.data.Add( new TestStruct() { x = 5, y = 6 } );
var testClassJson = JSON.Dump( testClass, true );
Console.WriteLine( testClassJson );
```
Will output:
```json
{
"name": "Rumpelstiltskin Jones",
"type": "Thing2",
"data": [
{
"x": 1,
"y": 2
},
{
"x": 3,
"y": 4
},
{
"x": 5,
"y": 6
}
]
}
```
You can use, `MakeInto()` can be used to reconstruct JSON data back into an object:
```csharp
TestClass testClass;
JSON.MakeInto( JSON.Load( testClassJson ), out testClass );
```
There are also `Make()` methods on `Variant` which provide options for slightly more natural syntax:
```csharp
TestClass testClass;
JSON.Load( json ).Make( out testClass );
// or
testClass = JSON.Load( json ).Make<Data>();
```
Finally, you'll notice that `TestClass` has the methods `BeforeEncode()` and `AfterDecode()` which have the `TinyJSON.BeforeEncode` and `TinyJSON.AfterDecode` attributes. These methods will be called *before* the object starts being serialized and *after* the object has been fully deserialized. This is useful when some further preparation or initialization logic is required.
By default, only public fields are encoded, not properties or private fields. You can tag any field or property to be included with the `TinyJSON.Include` attribute, or force a public field to be excluded with the `TinyJSON.Exclude` attribute.
## Decode Aliases
Fields and properties can be decoded from aliases using the `TinyJSON.DecodeAlias` attribute. While decoding, if no matching data is found in the JSON for a given field or property, its aliases will also be searched for.
```csharp
class TestClass
{
[DecodeAlias("anotherName")]
public string name; // decode from "name" or "anotherName"
[DecodeAlias("anotherNumber", "yetAnotherNumber")]
public int number; // decode from "number", "anotherNumber", or "yetAnotherNumber"
}
```
## Type Hinting
When decoding polymorphic types, TinyJSON has no way of knowing which subclass to instantiate unless a type hint is included. So, by default, TinyJSON will add a key named `@type` to each encoded object with the fully qualified type of the object.
## Encode Options
Several options are currently available for JSON encoding, and can be passed in as a second parameter to `JSON.Dump()`.
* `EncodeOptions.PrettyPrint` will output nicely formatted JSON to make it more readable.
* `EncodeOptions.NoTypeHints` will disable the outputting of type hints into the JSON output. This may be desirable if you plan to read the JSON into another application that might choke on the type information. You can override this on a per-member basis with the `TinyJSON.TypeHint` attribute.
* `EncodeOptions.IncludePublicProperties` will include public properties in the output.
* `EncodeOptions.EnforceHeirarchyOrder` will ensure fields and properties are encoded in class heirarchy order, from the root base class on down, but comes at a slight performance cost.
## Using Variants
For most use cases you can just assign, cast or make your object graph using the API outlined above, but at times you may need to work with the intermediate proxy objects to, say, dig through and iterate over a collection. To do this, cast the `Variant` to the appropriate subclass (likely either `ProxyArray` or `ProxyObject`) and you're good to go:
```csharp
var list = JSON.Load( "[1,2,3]" );
foreach (var item in list as ProxyArray)
{
int number = item;
Console.WriteLine( number );
}
var dict = JSON.Load( "{\"x\":1,\"y\":2}" );
foreach (var pair in dict as ProxyObject)
{
float value = pair.Value;
Console.WriteLine( pair.Key + " = " + value );
}
```
The non-collection `Variant` subclasses are `ProxyBoolean`, `ProxyNumber` and `ProxyString`. A variant can also be `null`.
Any `Variant` object can be directly encoded to JSON by calling its `ToJSON()` method or passing it to `JSON.Dump()`.
## Notes
This project was developed with pain elimination and lightweight size in mind. It should be able to handle reasonable amounts of reasonable data at reasonable speeds, but it's not meant for massive data sets.
The primary use case for this library is with Unity3D, so compatibility is focused there, though it should work with most modern C# environments.
It has been used in several published games. It's good for preferences, level and progress data, etc.
## Meta
Handcrafted by Patrick Hogan [[twitter](http://twitter.com/pbhogan) • [github](http://github.com/pbhogan) • [website](http://www.gallantgames.com)]
Based on [MiniJSON](https://gist.github.com/darktable/1411710) by Calvin Rien
Released under the [MIT License](http://www.opensource.org/licenses/mit-license.php).
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
一般信息: 普通控制台用于调试和显示插件和Mod的输出。 调试模式/控制台用于调试MelonLoader内部。 所有插件都放置在游戏的“安装”文件夹内已创建的“插件”文件夹中。 所有Mod都放置在游戏的Install文件夹内创建的Mods文件夹中。 所有日志均在游戏的“安装”文件夹内创建的日志文件夹中进行。 为了正确使用dnSpy的调试器,请使用“调试模式/控制台”启动选项运行游戏。 要求: 自动安装: 确保要安装的游戏已关闭且未运行。 下载 。 运行MelonLoader.Installer.exe。 单击选择按钮。 在游戏的安装文件夹中选择并打开游戏的EXE。 使用
资源详情
资源评论
资源推荐
收起资源包目录
MelonLoader:世界上第一个适用于Unity游戏的通用Mod加载器,可与Il2Cpp和Mono兼容 (596个子文件)
version.asm 2KB
Syntax.cd 12KB
Syntax.cd 12KB
disasm.cpp 159KB
detours.cpp 78KB
image.cpp 65KB
creatwth.cpp 55KB
modules.cpp 32KB
uimports.cpp 10KB
MelonLoader.cpp 8KB
Mono.cpp 7KB
Logger.cpp 7KB
Exports.cpp 6KB
HookManager.cpp 6KB
DllMain.cpp 4KB
DisableAnalytics.cpp 3KB
AssertionManager.cpp 2KB
MelonLoader_Base.cpp 2KB
Console.cpp 2KB
IL2CPP.cpp 985B
DllMain.cpp 455B
FindPattern.cpp 187B
UnityTLS.cpp 164B
disolarm64.cpp 60B
disolia64.cpp 59B
disolarm.cpp 58B
disolx64.cpp 58B
disolx86.cpp 58B
ZipFile.cs 131KB
Lexer.cs 42KB
BZip2OutputStream.cs 42KB
Lexer.cs 42KB
ZipEntry.cs 37KB
TarHeader.cs 32KB
ZipExtraData.cs 27KB
ZipOutputStream.cs 26KB
TarArchive.cs 25KB
DeflaterHuffman.cs 24KB
BZip2InputStream.cs 24KB
Inflater.cs 24KB
Main.cs 24KB
FastZip.cs 23KB
DeflaterEngine.cs 23KB
Parser.cs 23KB
Parser.cs 23KB
Program.cs 22KB
LzwInputStream.cs 21KB
MethodCopier.cs 21KB
InflaterInputStream.cs 20KB
TarInputStream.cs 20KB
ZipInputStream.cs 20KB
MelonHandler.cs 20KB
JsonValue.cs 19KB
JsonValue.cs 19KB
ZipConstants.cs 19KB
TarBuffer.cs 18KB
Deflater.cs 17KB
DeflaterOutputStream.cs 17KB
AssemblyGenerator.cs 17KB
ZipHelperStream.cs 17KB
SyntaxValidator.cs 16KB
SyntaxValidator.cs 16KB
TarOutputStream.cs 15KB
MethodPatcher.cs 15KB
TarEntry.cs 15KB
FileSystemScanner.cs 15KB
JSON.cs 15KB
JSON.cs 15KB
PkzipClassic.cs 14KB
PatchFunctions.cs 14KB
MainForm.Designer.cs 13KB
AccessTools.cs 13KB
ZipEntryFactory.cs 12KB
MelonBase.cs 11KB
GzipInputStream.cs 11KB
CodeTranspiler.cs 11KB
Encoder.cs 11KB
Encoder.cs 11KB
PathFilter.cs 10KB
CharHelper.cs 9KB
PatchProcessor.cs 9KB
Emitter.cs 9KB
NameFilter.cs 9KB
CharHelper.cs 9KB
Signature.cs 9KB
StreamManipulator.cs 9KB
CRC32.cs 9KB
StreamUtils.cs 9KB
ZipNameTransform.cs 8KB
WindowsNameTransform.cs 8KB
MelonPrefs.cs 8KB
PendingBuffer.cs 8KB
SyntaxNode.cs 8KB
SyntaxNode.cs 8KB
BZip2Constants.cs 8KB
JsonReader.cs 8KB
JsonReader.cs 8KB
Traverse.cs 8KB
GzipOutputStream.cs 8KB
Adler32.cs 7KB
共 596 条
- 1
- 2
- 3
- 4
- 5
- 6
徐志鹄
- 粉丝: 16
- 资源: 4661
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0