public async Task <IActionResult> UpdateDatamodel(string org, string app, string filepath) { SchemaKeywordCatalog.Add <InfoKeyword>(); using (Stream resource = Request.Body) { // Read the request body and deserialize to Json Schema using StreamReader streamReader = new StreamReader(resource); string content = await streamReader.ReadToEndAsync(); TextReader textReader = new StringReader(content); JsonValue jsonValue = await JsonValue.ParseAsync(textReader); JsonSchema jsonSchemas = new Manatee.Json.Serialization.JsonSerializer().Deserialize <JsonSchema>(jsonValue); // Serialize and store the Json Schema var serializer = new Manatee.Json.Serialization.JsonSerializer(); JsonValue toar = serializer.Serialize(jsonSchemas); byte[] byteArray = Encoding.UTF8.GetBytes(toar.ToString()); MemoryStream jsonstream = new MemoryStream(byteArray); await _repository.WriteData(org, app, $"{filepath}.schema.json", jsonstream); // Convert to XML Schema and store in repository JsonSchemaToXsd jsonSchemaToXsd = new JsonSchemaToXsd(); XmlSchema xmlschema = jsonSchemaToXsd.CreateXsd(jsonSchemas); MemoryStream xsdStream = new MemoryStream(); XmlTextWriter xwriter = new XmlTextWriter(xsdStream, new UpperCaseUtf8Encoding()); xwriter.Formatting = Formatting.Indented; xwriter.WriteStartDocument(false); xmlschema.Write(xsdStream); await _repository.WriteData(org, app, $"{filepath}.xsd", xsdStream); } return(Ok()); }
public async Task <IActionResult> GetDatamodel(string org, string repository, string filepath) { try { Stream dataStream = await _repository.ReadData(org, repository, $"{filepath}.schema.json"); TextReader textReader = new StreamReader(dataStream); JsonValue jsonValue = await JsonValue.ParseAsync(textReader); return(Ok(jsonValue.ToString())); } catch { // Will fallback to checking for XSD. See below } try { Stream dataStream = await _repository.ReadData(org, repository, $"{filepath}.xsd"); XmlReader xsdReader = XmlReader.Create(dataStream); XsdToJsonSchema xsdToJsonSchemaConverter = new XsdToJsonSchema(xsdReader); // Act JsonSchema convertedSchema = xsdToJsonSchemaConverter.AsJsonSchema(); Manatee.Json.Serialization.JsonSerializer serializer = new Manatee.Json.Serialization.JsonSerializer(); JsonValue serializedConvertedSchema = serializer.Serialize(convertedSchema); return(Ok(serializedConvertedSchema.ToString())); } catch { return(NotFound()); } }
public async Task <IActionResult> UpdateDatamodel(string org, string app, string modelName) { SchemaKeywordCatalog.Add <InfoKeyword>(); try { modelName = modelName.AsFileName(); } catch { return(BadRequest("Invalid model name value.")); } string filePath = $"App/models/{modelName}"; using (Stream resource = Request.Body) { // Read the request body and deserialize to Json Schema using StreamReader streamReader = new StreamReader(resource); string content = await streamReader.ReadToEndAsync(); TextReader textReader = new StringReader(content); JsonValue jsonValue = await JsonValue.ParseAsync(textReader); JsonSchema jsonSchemas = new Manatee.Json.Serialization.JsonSerializer().Deserialize <JsonSchema>(jsonValue); // Create the directory if it does not exist string appPath = _repository.GetAppPath(org, app); string directory = appPath + Path.GetDirectoryName(filePath); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } // Serialize and store the Json Schema var serializer = new Manatee.Json.Serialization.JsonSerializer(); JsonValue toar = serializer.Serialize(jsonSchemas); byte[] byteArray = Encoding.UTF8.GetBytes(toar.ToString()); MemoryStream jsonstream = new MemoryStream(byteArray); await _repository.WriteData(org, app, $"{filePath}.schema.json", jsonstream); // update meta data JsonSchemaToInstanceModelGenerator converter = new JsonSchemaToInstanceModelGenerator(org, app, jsonSchemas); ModelMetadata modelMetadata = converter.GetModelMetadata(); string root = modelMetadata.Elements != null && modelMetadata.Elements.Count > 0 ? modelMetadata.Elements.Values.First(e => e.ParentElement == null).TypeName : null; _repository.UpdateApplicationWithAppLogicModel(org, app, modelName, "Altinn.App.Models." + root); // Convert to XML Schema and store in repository JsonSchemaToXsd jsonSchemaToXsd = new JsonSchemaToXsd(); XmlSchema xmlschema = jsonSchemaToXsd.CreateXsd(jsonSchemas); MemoryStream xsdStream = new MemoryStream(); XmlTextWriter xwriter = new XmlTextWriter(xsdStream, new UpperCaseUtf8Encoding()); xwriter.Formatting = Formatting.Indented; xwriter.WriteStartDocument(false); xmlschema.Write(xsdStream); await _repository.WriteData(org, app, $"{filePath}.xsd", xsdStream); } return(Ok()); }
private void ValidateJsonManatee() { try { txtError.Text = ""; var serializer = new Manatee.Json.Serialization.JsonSerializer(); var json = Manatee.Json.JsonValue.Parse(txtJson.Text); var schemaJson = Manatee.Json.JsonValue.Parse(txtSchema.Text); var schema = new Manatee.Json.Schema.JsonSchema(); schema.FromJson(schemaJson, serializer); var options = new Manatee.Json.Schema.JsonSchemaOptions(); options.OutputFormat = Manatee.Json.Schema.SchemaValidationOutputFormat.Basic; var validationResults = schema.Validate(json, options); if (!validationResults.IsValid) { json = serializer.Serialize(validationResults); txtError.Text = JsonHelper.FormatJson(json.ToString()); } else { txtError.Text = "Valid"; } } catch (Exception ex) { txtError.Text = "Parse error: " + ex.Message; } }
public async Task <IActionResult> GetDatamodel(string org, string repository, string modelName) { try { modelName = modelName.AsFileName(); } catch { return(BadRequest("Invalid model name value.")); } string filePath = $"App/models/{modelName}"; try { using (Stream dataStream = await _repository.ReadData(org, repository, $"{filePath}.schema.json")) { TextReader textReader = new StreamReader(dataStream); JsonValue jsonValue = await JsonValue.ParseAsync(textReader); return(Ok(jsonValue.ToString())); } } catch { // Will fallback to checking for XSD. See below } try { using (Stream dataStream = await _repository.ReadData(org, repository, $"{filePath}.xsd")) { XmlReader xsdReader = XmlReader.Create(dataStream); XsdToJsonSchema xsdToJsonSchemaConverter = new XsdToJsonSchema(xsdReader); JsonSchema convertedSchema = xsdToJsonSchemaConverter.AsJsonSchema(); Manatee.Json.Serialization.JsonSerializer serializer = new Manatee.Json.Serialization.JsonSerializer(); JsonValue serializedConvertedSchema = serializer.Serialize(convertedSchema); return(Ok(serializedConvertedSchema.ToString())); } } catch { return(NotFound()); } }
private static string _ManateeSerialize(EmojiResponse obj) { _manateeWatch.Reset(); _manateeWatch.Start(); var json = _serializer.Serialize(obj); _manateeWatch.Stop(); _serializeTime += _manateeWatch.ElapsedTicks; _manateeWatch.Reset(); _manateeWatch.Start(); var str = json.ToString(); _manateeWatch.Stop(); _toStringTime += _manateeWatch.ElapsedTicks; return(str); }
public async Task <IActionResult> Schemas() { AltinnServiceRepository repositoryClient = new AltinnServiceRepository(); Task <List <AltinnResource> > serviceRequestTask = AltinnServiceRepository.ReadAllSchemas(); await Task.WhenAll(serviceRequestTask); if (serviceRequestTask.Result != null) { Manatee.Json.Serialization.JsonSerializer serializer = new Manatee.Json.Serialization.JsonSerializer(); JsonValue json = serializer.Serialize(serviceRequestTask.Result); return(Ok(json.GetIndentedString())); } return(NoContent()); }
public async Task <IActionResult> Convert() { XmlReaderSettings settings = new XmlReaderSettings { IgnoreWhitespace = true, }; if (Request.ContentType.Contains("text/xml")) { XmlReader doc = XmlReader.Create(Request.Body, settings); // XSD to Json Schema XsdToJsonSchema xsdToJsonSchemaConverter = new XsdToJsonSchema(doc, null); Manatee.Json.Serialization.JsonSerializer serializer = new Manatee.Json.Serialization.JsonSerializer(); JsonValue json = serializer.Serialize(xsdToJsonSchemaConverter.AsJsonSchema()); return(Ok(json.GetIndentedString())); } return(NotFound("Cannot read body. Needs to be XSD.")); }
private static string _ManateeSerialize(MyClass obj) { var json = _serializer.Serialize(obj); return(json.ToString()); }