Skip to content

Convert JSON objects to class definitions.

License

Notifications You must be signed in to change notification settings

Harsh-Geek/Json2Sharp

 
 

Repository files navigation

.NET Unit Tests CodeQL CodeFactor NuGet Downloads NuGet Badge NuGet Nightly Badge

Json2Sharp

Because life is too short to map data by hand.

Json2Sharp is a CLI application that converts a JSON object to a class definition (or an equivalent for the target language).

Currently, C# and Python are supported. We're open to contributions. If you'd like to add your favorite language to the mix, feel free to open a pull request!

Installation

If you have dotnet installed, you can install Json2Sharp as a NuGet tool.

dotnet tool install -g json2sharp-cli

Other packaging options are listed in the wiki.

Quick start

Pipe JSON data directly into Json2Sharp.

$ curl -s https://api.isevenapi.xyz/api/iseven/6 | json2sharp
using System.Text.Json.Serialization;

public sealed record Root(
    [property: JsonPropertyName("ad")] string Ad,
    [property: JsonPropertyName("iseven")] bool Iseven
);

Or pass the JSON data to the --json/-j option.

$ json2sharp -j "{ \"ad\": \"Some ad here\", \"iseven\": false }"
using System.Text.Json.Serialization;

public sealed record Root(
    [property: JsonPropertyName("ad")] string Ad,
    [property: JsonPropertyName("iseven")] bool Iseven
);

Or tell it to use a file as input.

$ json2sharp -i IsEven.json
using System.Text.Json.Serialization;

public sealed record Root(
    [property: JsonPropertyName("ad")] string Ad,
    [property: JsonPropertyName("iseven")] bool Iseven
);

You can also save the result to a file.

$ curl -s https://api.isevenapi.xyz/api/iseven/6 | json2sharp -o IsEven.cs
$ cat IsEven.cs
using System.Text.Json.Serialization;

public sealed record Root(
    [property: JsonPropertyName("ad")] string Ad,
    [property: JsonPropertyName("iseven")] bool Iseven
);

For additional options, please visit the wiki.

NuGet

Json2Sharp is also available as a library on NuGet. To add it to your .NET project, simply run the following command.

dotnet add package Json2Sharp

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; }
 * }
 */

License

Copyright 2023 Kotz

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

About

Convert JSON objects to class definitions.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%