Json2Sharp (1.1.11)

Published 2024-07-03 06:01:39 -07:00 by toastie_t0ast

Installation

dotnet nuget add source --name toastie_t0ast --username your_username --password your_token 
dotnet add package --source toastie_t0ast --version 1.1.11 Json2Sharp

About this package

Parse JSON data into language type declarations.

Json2Sharp

Json2Sharp is a library that converts a JSON object to a programming language type definition (i.e. a class).

To perform a conversion, call the Parse method from the Json2Sharp class.

string code = Json2Sharp.Parse("Person", """{ "id": 1, "name": "John" }""");
/*
 * using System.Text.Json.Serialization;
 *
 * public sealed record Person(
 *     [property: JsonPropertyName("id")] int Id,
 *     [property: JsonPropertyName("name")] bool Name
 * );
 */

You can also customize the conversion by initializing a Json2SharpOptions object and populating its members to suit your needs.

Json2SharpOptions options = new()
{
    TargetLanguage = Language.CSharp,
    CSharpOptions = new()
    {
        IsSealed = false,
        IsPropertyRequired = false,
        TargetType = CSharpObjectType.Class,
        SerializationAttribute = CSharpSerializationAttribute.NewtonsoftJson
    }
};

string code = Json2Sharp.Parse(className, rawJson, options);
/*
 * using Newtonsoft.Json;
 *
 * public sealed class Person
 * {
 *     [JsonProperty("id")]
 *     public int Id { get; init; }
 *
 *     [JsonProperty("name")]
 *     public string Name { get; init; }
 * }
 */
Details
NuGet
2024-07-03 06:01:39 -07:00
6
Toastie
65 KiB
Assets (4)
Versions (1) View all
1.1.11 2024-07-03