public static SchemaType.Context Generate(NJsonSchema.CodeGeneration.CSharp.CSharpTypeResolver types) { var context = new SchemaType.Context(); foreach (var t in types.Types.Keys) { context.UseType(t); } return(context); }
public void DeclareContext(SchemaType.Context context) { foreach (var ctype in context.Classes) { DeclareClass(ctype); } foreach (var etype in context.Enumerations) { DeclareEnum(etype); } }
public string EmitContext(SchemaType.Context context) { var sb = new StringBuilder(); sb.AppendLine("//------------------------------------------------------------------------------------------------"); sb.AppendLine("// This file has been programatically generated; DON´T EDIT!"); sb.AppendLine("//------------------------------------------------------------------------------------------------"); sb.AppendLine(); sb.AppendLine(); sb.AppendLine("using System;"); sb.AppendLine("using System.Collections.Generic;"); sb.AppendLine("using System.Linq;"); sb.AppendLine("using System.Text;"); sb.AppendLine("using System.Numerics;"); sb.AppendLine("using Newtonsoft.Json;"); sb.AppendLine(); sb.AppendLine($"namespace {Constants.OutputNamespace}"); sb.AppendLine("{"); sb.AppendLine("using Collections;".Indent(1)); sb.AppendLine(); foreach (var etype in context.Enumerations) { var cout = EmitEnum(etype); sb.AppendLine(cout); sb.AppendLine(); } foreach (var ctype in context.Classes) { var cout = EmitClass(ctype); sb.AppendLine(cout); sb.AppendLine(); } sb.AppendLine("}"); return(sb.ToString()); }
public static SchemaType UseType(this SchemaType.Context ctx, NJsonSchema.JsonSchema4 schema, bool isRequired = true) { if (ctx == null) { throw new ArgumentNullException(nameof(ctx)); } if (schema == null) { throw new ArgumentNullException(nameof(schema)); } if (schema is NJsonSchema.JsonProperty prop) { // System.Diagnostics.Debug.Assert(prop.Name != "scene"); isRequired &= prop.IsRequired; } if (_IsStringType(schema)) { return(ctx.UseString()); } if (_IsBlittableType(schema)) { bool isNullable = !isRequired; if (schema.Type == NJsonSchema.JsonObjectType.Integer) { return(ctx.UseBlittable(typeof(Int32).GetTypeInfo(), isNullable)); } if (schema.Type == NJsonSchema.JsonObjectType.Number) { return(ctx.UseBlittable(typeof(Double).GetTypeInfo(), isNullable)); } if (schema.Type == NJsonSchema.JsonObjectType.Boolean) { return(ctx.UseBlittable(typeof(Boolean).GetTypeInfo(), isNullable)); } throw new NotImplementedException(); } if (schema.HasReference) { return(ctx.UseType(schema.ActualTypeSchema, isRequired)); // we could have our own ref } if (schema.IsArray) { return(ctx.UseArray(ctx.UseType(schema.Item.ActualSchema))); } if (_IsEnumeration(schema)) { if (schema is NJsonSchema.JsonProperty property) { bool isNullable = !isRequired; var dict = new Dictionary <string, Int64>(); foreach (var v in property.AnyOf) { var val = v.Enumeration.FirstOrDefault(); var key = v.Description; if (val is String) { key = (string)val; val = (Int64)0; } if (string.IsNullOrWhiteSpace(key)) { continue; } dict[key] = (Int64)val; } // JSon Schema AnyOf enumerations are basically anonymous, so we create // a "shared name" with a concatenation of all the values: var name = string.Join("-", dict.Keys.OrderBy(item => item)); var etype = ctx.UseEnum(name, isNullable); foreach (var kvp in dict) { etype.SetValue(kvp.Key, (int)kvp.Value); } if (dict.Values.Distinct().Count() > 1) { etype.UseIntegers = true; } return(etype); } throw new NotImplementedException(); } if (_IsDictionary(schema)) { var key = ctx.UseString(); var val = ctx.UseType(_GetDictionaryValue(schema)); return(ctx.UseDictionary(key, val)); } if (_IsClass(schema)) { var classDecl = ctx.UseClass(schema.Title); // process base class if (schema.InheritedSchema != null) { classDecl.BaseClass = ctx.UseType(schema.InheritedSchema) as ClassType; } // filter declared properties var keys = _GetProperyNames(schema); if (schema.InheritedSchema != null) // filter our parent properties { var baseKeys = _GetInheritedPropertyNames(schema).ToArray(); keys = keys.Except(baseKeys).ToArray(); } // process declared properties var props = keys.Select(key => schema.Properties.Values.FirstOrDefault(item => item.Name == key)); var required = schema.RequiredProperties; // System.Diagnostics.Debug.Assert(schema.Title != "Buffer View"); foreach (var p in props) { var field = classDecl.UseField(p.Name); field.FieldType = ctx.UseType(p, required.Contains(p.Name)); field.MinimumValue = p.Minimum; field.MaximumValue = p.Maximum; field.DefaultValue = p.Default; field.MinItems = p.MinItems; field.MaxItems = p.MaxItems; } return(classDecl); } if (schema.Type == NJsonSchema.JsonObjectType.Object) { return(ctx.UseAnyType()); } if (schema.Type == NJsonSchema.JsonObjectType.None) { return(ctx.UseAnyType()); } throw new NotImplementedException(); }
private static void ProcessSchema(string dstFile, SchemaType.Context ctx) { var newEmitter = new CSharpEmitter(); newEmitter.DeclareContext(ctx); newEmitter.SetCollectionContainer("List<TItem>"); const string rootName = "ModelRoot"; newEmitter.SetRuntimeName("glTF", rootName); newEmitter.SetRuntimeName("glTF Property", "ExtensionsProperty"); newEmitter.SetRuntimeName("glTF Child of Root Property", "LogicalChildOfRoot"); // newEmitter.SetRuntimeName("Sampler", "TextureSampler"); newEmitter.SetRuntimeName("UNSIGNED_BYTE-UNSIGNED_INT-UNSIGNED_SHORT", "IndexType"); newEmitter.SetRuntimeName("BYTE-FLOAT-SHORT-UNSIGNED_BYTE-UNSIGNED_INT-UNSIGNED_SHORT", "ComponentType"); newEmitter.SetRuntimeName("MAT2-MAT3-MAT4-SCALAR-VEC2-VEC3-VEC4", "ElementType"); newEmitter.SetRuntimeName("rotation-scale-translation-weights", "PathType"); newEmitter.SetRuntimeName("ARRAY_BUFFER-ELEMENT_ARRAY_BUFFER", "BufferMode"); newEmitter.SetRuntimeName("orthographic-perspective", "CameraType"); newEmitter.SetRuntimeName("BLEND-MASK-OPAQUE", "AlphaMode"); newEmitter.SetRuntimeName("LINE_LOOP-LINE_STRIP-LINES-POINTS-TRIANGLE_FAN-TRIANGLE_STRIP-TRIANGLES", "PrimitiveType"); newEmitter.SetRuntimeName("CUBICSPLINE-LINEAR-STEP", "AnimationInterpolationMode"); newEmitter.SetRuntimeName("LINEAR-NEAREST", "TextureInterpolationMode"); newEmitter.SetRuntimeName("CLAMP_TO_EDGE-MIRRORED_REPEAT-REPEAT", "TextureWrapMode"); newEmitter.SetRuntimeName("LINEAR-LINEAR_MIPMAP_LINEAR-LINEAR_MIPMAP_NEAREST-NEAREST-NEAREST_MIPMAP_LINEAR-NEAREST_MIPMAP_NEAREST", "TextureMipMapMode"); newEmitter.SetRuntimeName("KHR_materials_pbrSpecularGlossiness glTF extension", "MaterialPBRSpecularGlossiness_KHR"); newEmitter.SetRuntimeName("KHR_materials_unlit glTF extension", "MaterialUnlit_KHR"); var classes = ctx.Classes.ToArray(); var fields = classes.SelectMany(item => item.Fields).ToArray(); var meshClass = classes.FirstOrDefault(c => c.PersistentName == "Mesh"); if (meshClass != null) { newEmitter.SetCollectionContainer(meshClass.UseField("primitives"), "ChildrenCollection<TItem,Mesh>"); } var animationClass = classes.FirstOrDefault(c => c.PersistentName == "Animation"); if (animationClass != null) { newEmitter.SetCollectionContainer(animationClass.UseField("channels"), "ChildrenCollection<TItem,Animation>"); newEmitter.SetCollectionContainer(animationClass.UseField("samplers"), "ChildrenCollection<TItem,Animation>"); } foreach (var f in fields) { if (f.FieldType is ArrayType atype) { if (atype.ItemType is ClassType ctype) { if (ctype.BaseClass != null && ctype.BaseClass.PersistentName == "glTF Child of Root Property") { newEmitter.SetCollectionContainer(f, $"ChildrenCollection<TItem,{rootName}>"); } } } } var textOut = newEmitter.EmitContext(ctx); var dstDir = _FindTargetDirectory(Constants.TargetProjectDirectory); var dstPath = System.IO.Path.Combine(dstDir, $"{dstFile}.cs"); System.IO.File.WriteAllText(dstPath, textOut); }