Created
December 4, 2019 10:22
-
-
Save Rodbourn/691e5277f061b26189820c03504a544b to your computer and use it in GitHub Desktop.
Convert Microsoft.Data.Edm.IEdmModel to CSDL JSON suited for breezejs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.IO; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Xml; | |
| using Microsoft.Data.Edm; | |
| using Microsoft.Data.Edm.Csdl; | |
| using Newtonsoft.Json; | |
| using Newtonsoft.Json.Linq; | |
| using Formatting = Newtonsoft.Json.Formatting; | |
| namespace Common | |
| { | |
| public static class IEdmModelToCsdlJsonExtension | |
| { | |
| /// <summary> | |
| /// Get CSDL Serialized to JSON suited for BreezeJS to import as metadata | |
| /// </summary> | |
| /// <param name="model"></param> | |
| /// <param name="formatting"></param> | |
| /// <returns></returns> | |
| public static string ToCsdlJson(this IEdmModel model, Formatting formatting = Formatting.Indented) | |
| { | |
| // first serialize the csdl as xml | |
| var xmlStringBuilder = new StringBuilder(); | |
| using (var writer = XmlWriter.Create(xmlStringBuilder, new XmlWriterSettings())) | |
| { | |
| if (!model.TryWriteCsdl(writer, out var errors) || errors.Any()) | |
| return null; | |
| } | |
| var document = new XmlDocument(); | |
| document.LoadXml(xmlStringBuilder.ToString()); | |
| // now serialize it to json while using camel case and removing @ and # prefixes | |
| var jsonStringBuilder = new StringBuilder(); | |
| JsonSerializer.Create().Serialize(new CustomJsonWriter(new StringWriter(jsonStringBuilder)), document); | |
| // now deserialize into a JObject to remove xml artifacts | |
| var csdlJson = JObject.Parse(jsonStringBuilder.ToString()); | |
| csdlJson.Remove("?xml"); | |
| var search = csdlJson.Descendants().Where(i => i.Path.Contains("xmlns")).ToList(); | |
| foreach (var item in search) | |
| if (item.Type == JTokenType.Property || item.Type == JTokenType.Object) | |
| item.Remove(); | |
| return csdlJson.ToString(formatting); | |
| } | |
| public static string ToCamel(this string input) | |
| { | |
| string newString = input; | |
| if (!string.IsNullOrEmpty(newString) && char.IsUpper(newString[0])) | |
| newString = char.ToLower(newString[0]) + newString.Substring(1); | |
| return newString; | |
| } | |
| private class CustomJsonWriter : JsonTextWriter | |
| { | |
| public CustomJsonWriter(TextWriter writer) : base(writer) | |
| { | |
| } | |
| public override void WritePropertyName(string name) | |
| { | |
| if (name.StartsWith("@") || name.StartsWith("#")) | |
| base.WritePropertyName(name.Substring(1).ToCamel()); | |
| else | |
| base.WritePropertyName(name.ToCamel()); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment