All Projects → hez2010 → TypedocConverter

hez2010 / TypedocConverter

Licence: MIT license
This is a typedoc json to C# type bindings converter. Can be used as a TypeScript to C# bindings converter.

Programming Languages

F#
602 projects

Projects that are alternatives of or similar to TypedocConverter

kubernate
Kubernetes+Generate = Kubernate❤️
Stars: ✭ 96 (+7.87%)
Mutual labels:  codegen
graphql-java-codegen-gradle-plugin
Gradle plugin for graphql-java-codegen
Stars: ✭ 19 (-78.65%)
Mutual labels:  codegen
gqty
a GraphQL client built for rapid iteration
Stars: ✭ 420 (+371.91%)
Mutual labels:  codegen
cmake-reflection-template
A template for simple C++ reflection done with CMake and Python (no other external tools)
Stars: ✭ 37 (-58.43%)
Mutual labels:  codegen
graphql-ts-client
Typescript DSL for GraphQL.
Stars: ✭ 124 (+39.33%)
Mutual labels:  codegen
rosetta
Easy to use Rust i18n library based on code generation
Stars: ✭ 37 (-58.43%)
Mutual labels:  codegen
vox
Vox language compiler. AOT / JIT / Linker. Zero dependencies
Stars: ✭ 288 (+223.6%)
Mutual labels:  codegen
aws-mobile-appsync-sdk-android
Android SDK for AWS AppSync.
Stars: ✭ 101 (+13.48%)
Mutual labels:  codegen
ogen
OpenAPI v3 code generator for go
Stars: ✭ 436 (+389.89%)
Mutual labels:  codegen
EasyEE-Auto
EasyEE 自动化代码生成器。EasyEE Automated code generator.
Stars: ✭ 39 (-56.18%)
Mutual labels:  codegen
startle
Startle C Library
Stars: ✭ 15 (-83.15%)
Mutual labels:  codegen
graphql-cli-codegen
apollo-codegen plugin for graphql-cli
Stars: ✭ 22 (-75.28%)
Mutual labels:  codegen
graphql-codegen-apollo-next-ssr
Autogenerate apollo code for nextjs ssr
Stars: ✭ 176 (+97.75%)
Mutual labels:  codegen
d2a
A translator Django into SQLAlchemy.
Stars: ✭ 23 (-74.16%)
Mutual labels:  codegen
sbt-guardrail
Principled code generation from OpenAPI specifications
Stars: ✭ 24 (-73.03%)
Mutual labels:  codegen
DotNetJS
Consume C# in JavaScript with comfort: single-file UMD library, auto-generated 2-way bindings and type definitions
Stars: ✭ 551 (+519.1%)
Mutual labels:  codegen
typeplate
REST API boilerplate with Typescript, Express.js, Typeorm and Mocha.
Stars: ✭ 268 (+201.12%)
Mutual labels:  typedoc
codegen
Generator for dart_native bindings. Codegen can transform native SDK to Flutter plugin.
Stars: ✭ 75 (-15.73%)
Mutual labels:  codegen
qpmodel
A Relational Optimizer and Executor
Stars: ✭ 54 (-39.33%)
Mutual labels:  codegen
domainrobot-api
Swagger documentation for different APIs powered by InterNetX GmbH.
Stars: ✭ 14 (-84.27%)
Mutual labels:  codegen

Typedoc Converter

This is a typedoc json to C# bindings converter. Can be used as a TypeScript to C# bindings converter.

Build status: .NET

Compatibility Status

  • typedoc: 0.20
  • TypeScript: 3.9, 4.0, 4.1, 4.2

Languages support

  • Enums
    • Direct value
    • Referenced value
  • Interfaces
    • Inherits
    • Generics
    • Generics Constaints
    • Properties
    • Methods
    • Events
  • Classes
    • Constructors
    • Inherits
    • Generics
    • Generics Constaints
    • Properties
    • Methods
    • Events
    • Indexer
  • Types
    • String literals
    • Type literals
    • Type alias*
    • Union types
    • Intersection types
  • Split entities to different files
  • Auto rename conflict parameter names
  • WinRT/CLR async types support (IAsyncAction/IAsyncOperation<T> or Task/Task<T>)
  • Newtonsoft.Json and System.Text.Json for JSON serialization
  • Nullable Reference Types
  • Type validation

*: Partial support

Quick start

TypeScript code input.ts:

declare namespace test {
  /**
   * The declaration of an enum
   */
  export enum MyEnum {
    A = 0,
    B = 1,
    C = 2,
    D = C
  }

  /**
   * The declaration of an interface
   */
  export interface MyInterface1 {
    /**
     * A method
     */
    testMethod(arg: string, callback: () => void): string;
    /**
     * An event
     * @event
     */
    onTest(listener: (e: MyInterface1) => void): void;
    /**
     * An property
     */
    readonly testProp: string;
  }

  /**
   * Another declaration of an interface
   */
  export interface MyInterface2<T> {
    /**
     * A method
     */
    testMethod(arg: T, callback: () => void): T;
    /**
     * An event
     * @event
     */
    onTest(listener: (e: MyInterface2<T>) => void): void;
    /**
     * An property
     */
    readonly testProp: T;
  }

  /**
   * The declaration of a class
   */
  export class MyClass1<T> implements MyInterface1 {
    /**
     * A method
     */
    testMethod(arg: string, callback: () => void): string;
    /**
     * An event
     * @event
     */
    onTest(listener: (e: MyInterface1) => void): void;
    /**
     * An property
     */
    readonly testProp: string;
    static staticMethod(value: string, isOption?: boolean): UnionStr;
  }

  /**
   * Another declaration of a class
   */
  export class MyClass2<T> implements MyInterface2<T> {
    /**
     * A method
     */
    testMethod(arg: T, callback: () => void): T;
    /**
     * An event
     * @event
     */
    onTest(listener: (e: MyInterface2<T>) => void): void;
    /**
     * An property
     */
    readonly testProp: T;
    static staticMethod(value: string, isOption?: boolean): UnionStr;
  }

  /**
   * The declaration of a type alias
   */
  export type UnionStr = "A" | "B" | "C" | "other";
}

With below commands (need a tsconfig.json):

typedoc input.ts --json output.json
TypedocConverter --inputfile output.json --outputfile output.cs

Voila! C# type bindings generated in output.cs:

namespace Test
{

    /// <summary>
    /// The declaration of an enum
    /// </summary>
    enum MyEnum
    {
        A = 0,
        B = 1,
        C = 2,
        D = 2
    }
}

namespace Test
{

    /// <summary>
    /// The declaration of a class
    /// </summary>
    class MyClass1<T> : MyInterface1
    {
        /// <summary>
        /// An event
        /// </summary>
        public event System.Action<MyInterface1> OnTest;

        /// <summary>
        /// An property
        /// </summary>
        [Newtonsoft.Json.JsonProperty("testProp", NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public string TestProp { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); }

        /// <summary>
        /// A method
        /// </summary>
        public string TestMethod(string arg, System.Action callback) => throw new System.NotImplementedException();

        static UnionStr StaticMethod(string value, bool isOption) => throw new System.NotImplementedException();

    }
}

namespace Test
{

    /// <summary>
    /// Another declaration of a class
    /// </summary>
    class MyClass2<T> : MyInterface2<T>
    {
        /// <summary>
        /// An event
        /// </summary>
        public event System.Action<MyInterface2<T>> OnTest;

        /// <summary>
        /// An property
        /// </summary>
        [Newtonsoft.Json.JsonProperty("testProp", NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public T TestProp { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); }

        /// <summary>
        /// A method
        /// </summary>
        public T TestMethod(T arg, System.Action callback) => throw new System.NotImplementedException();

        static UnionStr StaticMethod(string value, bool isOption) => throw new System.NotImplementedException();

    }
}

namespace Test
{

    /// <summary>
    /// The declaration of an interface
    /// </summary>
    interface MyInterface1
    {
        /// <summary>
        /// An event
        /// </summary>
        event System.Action<MyInterface1> OnTest;

        /// <summary>
        /// An property
        /// </summary>
        [Newtonsoft.Json.JsonProperty("testProp", NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        string TestProp { get; set; }

        /// <summary>
        /// A method
        /// </summary>
        string TestMethod(string arg, System.Action callback);

    }
}

namespace Test
{

    /// <summary>
    /// Another declaration of an interface
    /// </summary>
    interface MyInterface2<T>
    {
        /// <summary>
        /// An event
        /// </summary>
        event System.Action<MyInterface2<T>> OnTest;

        /// <summary>
        /// An property
        /// </summary>
        [Newtonsoft.Json.JsonProperty("testProp", NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        T TestProp { get; set; }

        /// <summary>
        /// A method
        /// </summary>
        T TestMethod(T arg, System.Action callback);

    }
}

namespace Test
{

    /// <summary>
    /// The declaration of a type alias
    /// </summary>
    [Newtonsoft.Json.JsonConverter(typeof(UnionStrConverter))]
    enum UnionStr
    {
        ///<summary>
        /// A
        ///</summary>
        A,
        ///<summary>
        /// B
        ///</summary>
        B,
        ///<summary>
        /// C
        ///</summary>
        C,
        ///<summary>
        /// other
        ///</summary>
        Other
    }

    class UnionStrConverter : Newtonsoft.Json.JsonConverter
    {
        public override bool CanConvert(System.Type t) => t == typeof(UnionStr) || t == typeof(UnionStr?);

        public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type t, object? existingValue, Newtonsoft.Json.JsonSerializer serializer)
            => reader.TokenType switch
            {
                Newtonsoft.Json.JsonToken.String =>
                    serializer.Deserialize<string>(reader) switch
                    {
                        "A" => UnionStr.A,
                        "B" => UnionStr.B,
                        "C" => UnionStr.C,
                        "other" => UnionStr.Other,
                        _ => throw new System.NotSupportedException("Cannot unmarshal type UnionStr")
                    },
                _ => throw new System.NotSupportedException("Cannot unmarshal type UnionStr")
            };

        public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object? untypedValue, Newtonsoft.Json.JsonSerializer serializer)
        {
            if (untypedValue is null) { serializer.Serialize(writer, null); return; }
            var value = (UnionStr)untypedValue;
            switch (value)
            {
                case UnionStr.A: serializer.Serialize(writer, "A"); return;
                case UnionStr.B: serializer.Serialize(writer, "B"); return;
                case UnionStr.C: serializer.Serialize(writer, "C"); return;
                case UnionStr.Other: serializer.Serialize(writer, "other"); return;
                default: break;
            }
            throw new System.NotSupportedException("Cannot marshal type UnionStr");
        }
    }
}

Build

cd TypedocConverter/TypedocConverter
dotnet publish -c Release -r win-x64 --no-self-contained /p:PublishSingleFile=true /p:PublishReadyToRun=true

You can replace win-x64 with other platform identifiers such as win-arm64, linux-x64, linux-arm64, osx-x64 and etc.
Then built dists will be placed in bin/Release/net6.0/[platform identifier]/publish

Native Build

cd TypedocConverter/TypedocConverter
dotnet publish -c Release -r win-x64 /p:NativeBuild=true

You can replace win-x64 with other platform identifiers such as win-arm64, linux-x64, linux-arm64, osx-x64 and etc.
Then built dists will be placed in bin/Release/net6.0/[platform identifier]/publish

Run & Usage

TypedocConverter --help

Sample:

TypedocConverter --inputfile 1.json --splitfiles true --outputdir .

Arguments:

--inputfile [file]: input file
--namespace [namespace]: specify namespace for generated code
--splitfiles [true|false]: whether to split code to different files
--outputdir [path]: used for place code files when splitfiles is true
--outputfile [path]: used for place code file when splitfiles is false
--number-type [int/decimal/double...]: config for number type mapping
--promise-type [CLR/WinRT]: config for promise type mapping, CLR for Task and WinRT for IAsyncAction/IAsyncOperation
--any-type [object/dynamic...]: config for any type mapping
--array-type [Array/IEnumerable/List...]: config for array type mapping
--nrt-disabled [true|false]: whether to disable Nullable Reference Types
--use-system-json [true|false]: whether to use System.Text.Json instead of Newtonsoft.Json

Prebuilt binaries

We have prepared some prebuilt binaries for Windows, Linux and macOS.
You can download them directly from Releases

Prerequisites for non-native binaries: .NET Runtime 6.0

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].