GithubHelp home page GithubHelp logo

xbei.dynamicjson's Introduction

动态解析JSON

开发测试中

说明

有时候(特别是写和第三方通信的接口或者相关组件时)用来传输数据的JSON文件格式不稳定,另外当前程序只需要部分字段,这时候使用实体数据类,如果定义不完整,然后进行反系列化在系列化等操作以后,数据就会丢失。 这时候就需要动态解析JSON了。

示例JSON

{
  "Name": "WMB",
  "age": 42,
  "CreateTime": "2023-10-13T21:26:22+08:00"
}

使用示例:

    [JsonConverter(typeof(net.xBei.DynamicJson.Converters.DynamicJsonConverter<Car>))]
    class Car : net.xBei.DynamicJson.DynamicJson {
        public Car() : base(default) { }
        public string Name { get => GetString(nameof(Name)) ?? ""; set => SetString(nameof(Name), value); }
        public DateTime? CreateTime {
            get => GetDateTime(nameof(CreateTime));
            set => SetDateTime(nameof(CreateTime), value);
        }
    }

    var car = json.TryDeserialize<Car>() ?? throw new Exception("");
    car.Name = "福克斯(Focus)"
    Console.WriteLine(car.ToJson(true));

Car虽然没有定义age字段,但是在反序列化时,会自动忽略掉,而在序列化时,会自动添加上去。

输出结果:

{
  "Name": "福克斯(Focus)",
  "age": 42,
  "CreateTime": "2023-10-13T21:26:22+08:00"
}

注意

  1. 开发测试中
  2. 还不支持复杂数据类型

xbei.dynamicjson's People

Contributors

koaqiu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.