2 Star 12 Fork 2

Foldcc / FJson

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Fjson

Fjson是一套高易用Json解析库,无需对待序列化/反序列化的对象做特殊书写规范和约束

缺点:效率不高,不适合大量密集型数据处理。

序列化

FJsonUtility.ToJson(object obj)

Fjson支持序列化大部分常用类型,可以直接对List、数组、键值对、字典、自定义对象、结构体进行序列化

序列化示例1

Vector3 pos = new Vector3(100,1.5f,-50);

string json = FJsonUtility.ToJson(pos);

序列化示例2

List<object> listObj = new List<object>(new object[]{null , 1 , false , "test"});

string json = FJsonUtility.ToJson(listObj);

反序列化

FJsonUtility.ToObject(string json)

Fjson可以直接将json反序列化为List、数组、键值对、字典、自定义对象、结构体。

反序列化示例1

Dictionary<string, object> jsonObject = FJsonUtility.ToObject<Dictionary<string, object>>(json);
  • 注: 如果反序列化目标类型是Object,则序列化默认赋值类型是JsonObject\JsonArray

对象转换

FJsonUtility.Convert<T>(object value)

Fjson支持结构体、List、数组、Dictionary等对象\结构体相互转换,如一个Dictionary<string,object>转换为一个自定义对象,或者Array转换为一个List等

此外如果一个对象中的x字段为float类型,而转换目标中x字段为string或者int类型则会自动转换类型。

以下示例代码:

1 定义自定义类型

    public class Person
    {
        public string name;
        public double ID;
        private string info;
        public object[] infoList;

        public override string ToString()
        {
            return $"name {this.name} , id: {this.ID}";
        }
    }

2 创建一个Dictionary并转换为Object

Dictionary<string,object> dictObj = new Dictionary<string, object>();
            dictObj.Add("name" , "测试");
            dictObj.Add("ID" , 5321);
            dictObj.Add("info" , "测试info");
            dictObj.Add("infoList" , new object[]{1,-1,null});
var person = FJsonUtility.Convert<Person>(dictObj);
Console.WriteLine(person);
MIT License Copyright (c) 2019 Foldcc Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

一套针对Unity开发的的高易用性、扩展性Json序列库。 展开 收起
C#
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C#
1
https://gitee.com/Foldcc/FJson.git
git@gitee.com:Foldcc/FJson.git
Foldcc
FJson
FJson
master

搜索帮助