Skip to content

Instantly share code, notes, and snippets.

@dijeferson
Last active September 22, 2015 16:58
Show Gist options
  • Save dijeferson/fe26606e5b39d18fc196 to your computer and use it in GitHub Desktop.
Save dijeferson/fe26606e5b39d18fc196 to your computer and use it in GitHub Desktop.
A simple script to convert a json structure to csharp class with NewtonSoft json annotations, and correct casing.
# ParseJsonToCSharp
# A simple script to convert a json structure to csharp class with NewtonSoft json annotations, and correct casing.
# Author: Jeferson Santos (@dijeferson)
# Date: Sept 21st 2015
NAMESPACE_NAME = 'NamespaceHere'
CLASS_NAME = 'NomeDaClasse'
INPUT_FILE_PATH = 'c:/tmp/input.json'
OUTPUT_FILE_PATH = 'c:/tmp/output.cs'
### DO NOT CHANGE ANYTHING BELOW THIS LINE ###
import json
import string
jsonfile = open(INPUT_FILE_PATH)
fields = json.loads(jsonfile.read())
csharp = """
using Newtonsoft.Json;
using System;
namespace %s
{
public class %s : BaseServiceDomain
{
""" % (NAMESPACE_NAME, CLASS_NAME)
for item in fields:
csharp += '\t\t[JsonProperty(PropertyName = "%s")] \n' % item
csharp += '\t\tpublic string %s { get; set; } \n\n' % item.title().translate({ord('_'): None})
csharp += '\t}\n}'
f = open(OUTPUT_FILE_PATH, 'w')
f.write(csharp)
f.close()
jsonfile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment