xBei.DynamicJson 0.0.2-beta

This is a prerelease version of xBei.DynamicJson.
dotnet add package xBei.DynamicJson --version 0.0.2-beta                
NuGet\Install-Package xBei.DynamicJson -Version 0.0.2-beta                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="xBei.DynamicJson" Version="0.0.2-beta" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add xBei.DynamicJson --version 0.0.2-beta                
#r "nuget: xBei.DynamicJson, 0.0.2-beta"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install xBei.DynamicJson as a Cake Addin
#addin nuget:?package=xBei.DynamicJson&version=0.0.2-beta&prerelease

// Install xBei.DynamicJson as a Cake Tool
#tool nuget:?package=xBei.DynamicJson&version=0.0.2-beta&prerelease                

动态解析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. 还不支持复杂数据类型
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.2-beta 132 10/14/2023
0.0.1-beta 80 10/14/2023