snippetcsharpMinor
Convert XSD to JSONSchema
Viewed 0 times
convertjsonschemaxsd
Problem
This code is still very naive. I am trying to convert an existing XSD to JSONSchema. Firstly, pardon the variable names (I know some are really just stupid but I will fix them once I get all the functionality to work.)
```
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Schema;
using Newtonsoft.Json;
namespace XSDToJson
{
public class ReadXsd
{
private static string GetTargetSchema(string filepath)
{
var doc = new XmlDocument();
doc.Load(filepath);
return doc.OfType().First().Attributes["targetNamespace"].Value;
}
public static void ValidationCallback(object sender, ValidationEventArgs args)
{
switch (args.Severity)
{
case XmlSeverityType.Warning:
Console.Write("WARNING: ");
break;
case XmlSeverityType.Error:
Console.Write("ERROR: ");
break;
}
Console.WriteLine(args.Message);
}
private static Dictionary HandleSimpleTypeRestriction(XmlSchemaSimpleTypeRestriction restriction)
{
var res = new Dictionary();
foreach (var t in restriction.Facets.OfType())
{
var d = new Dictionary{{"fixed", t.IsFixed}};
var annotation = HandleAnnotation(t.Annotation);
if (annotation != "")
d.Add("description", annotation);
res.Add(t.Value,d);
}
foreach (var t in restriction.Facets.OfType())
{
var d = new Dictionary { { "fixed", t.IsFixed } };
var annotation = HandleAnnotation(t.Annotation);
if (annotation != "")
d.Add("description", annotation);
res.Add(t.Value, d);
}
```
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Schema;
using Newtonsoft.Json;
namespace XSDToJson
{
public class ReadXsd
{
private static string GetTargetSchema(string filepath)
{
var doc = new XmlDocument();
doc.Load(filepath);
return doc.OfType().First().Attributes["targetNamespace"].Value;
}
public static void ValidationCallback(object sender, ValidationEventArgs args)
{
switch (args.Severity)
{
case XmlSeverityType.Warning:
Console.Write("WARNING: ");
break;
case XmlSeverityType.Error:
Console.Write("ERROR: ");
break;
}
Console.WriteLine(args.Message);
}
private static Dictionary HandleSimpleTypeRestriction(XmlSchemaSimpleTypeRestriction restriction)
{
var res = new Dictionary();
foreach (var t in restriction.Facets.OfType())
{
var d = new Dictionary{{"fixed", t.IsFixed}};
var annotation = HandleAnnotation(t.Annotation);
if (annotation != "")
d.Add("description", annotation);
res.Add(t.Value,d);
}
foreach (var t in restriction.Facets.OfType())
{
var d = new Dictionary { { "fixed", t.IsFixed } };
var annotation = HandleAnnotation(t.Annotation);
if (annotation != "")
d.Add("description", annotation);
res.Add(t.Value, d);
}
Solution
I have not tried or scrutinized your code much, but my immediate impression is that you can clean this up quite a bit.
I recommend getting this book about clean code:
http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882
If you look at your HandleSimpleTypeRestriction method, you repeat the same 5 lines for each foreach statement. By extracting those five lines to a method, you'll shorten the HandleSimpleTypeRestriction method by 12 lines. The code will end up like this:
You could even put the for in a method so you end up with an even shorter method.
Rinse and repeat for all your code, and I bet you've got a much more readable class.
Then you can see if some of the methods share parameters that others don't use, and separate those into smaller utility classes with one responsibility.
See http://en.wikipedia.org/wiki/Single_responsibility_principle
(a very good practice)
All this gives you code that is easier to understand and maintain.
Should be done with care though, if you don't do unit testing, look into it! :)
I recommend getting this book about clean code:
http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882
If you look at your HandleSimpleTypeRestriction method, you repeat the same 5 lines for each foreach statement. By extracting those five lines to a method, you'll shorten the HandleSimpleTypeRestriction method by 12 lines. The code will end up like this:
private static Dictionary HandleSimpleTypeRestriction(XmlSchemaSimpleTypeRestriction restriction)
{
var res = new Dictionary();
foreach (var t in restriction.Facets.OfType())
AddRes(res, t);
foreach (var t in restriction.Facets.OfType())
AddRes(res, t);
foreach (var t in restriction.Facets.OfType())
AddRes(res, t);
return res;
}
private static void AddRes(Dictionary res, T t)
// you're gonna need a where T : somebaseclass here.
{
var d = new Dictionary{{"fixed", t.IsFixed}};
var annotation = HandleAnnotation(t.Annotation);
if (annotation != "")
d.Add("description", annotation);
res.Add(t.Value,d);
}You could even put the for in a method so you end up with an even shorter method.
Rinse and repeat for all your code, and I bet you've got a much more readable class.
Then you can see if some of the methods share parameters that others don't use, and separate those into smaller utility classes with one responsibility.
See http://en.wikipedia.org/wiki/Single_responsibility_principle
(a very good practice)
All this gives you code that is easier to understand and maintain.
Should be done with care though, if you don't do unit testing, look into it! :)
Code Snippets
private static Dictionary<String, Object> HandleSimpleTypeRestriction(XmlSchemaSimpleTypeRestriction restriction)
{
var res = new Dictionary<String, Object>();
foreach (var t in restriction.Facets.OfType<XmlSchemaEnumerationFacet>())
AddRes(res, t);
foreach (var t in restriction.Facets.OfType<XmlSchemaMaxExclusiveFacet>())
AddRes(res, t);
foreach (var t in restriction.Facets.OfType<XmlSchemaMinExclusiveFacet>())
AddRes(res, t);
return res;
}
private static void AddRes<T>(Dictionary<string, object> res, T t)
// you're gonna need a where T : somebaseclass here.
{
var d = new Dictionary<String, Object>{{"fixed", t.IsFixed}};
var annotation = HandleAnnotation(t.Annotation);
if (annotation != "")
d.Add("description", annotation);
res.Add(t.Value,d);
}Context
StackExchange Code Review Q#6959, answer score: 4
Revisions (0)
No revisions yet.