Created
July 17, 2012 02:20
-
-
Save mikeobrien/3126580 to your computer and use it in GitHub Desktop.
Serialize Expando
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
public class ExpandoJsonWriter : IJsonWriter | |
{ | |
private readonly IOutputWriter _outputWriter; | |
private readonly JavaScriptSerializer _jsonSerializer; | |
public ExpandoJsonWriter(IOutputWriter outputWriter) | |
{ | |
_outputWriter = outputWriter; | |
_jsonSerializer = new JavaScriptSerializer(); | |
} | |
public void Write(object output) | |
{ | |
Write(output, MimeType.Json.ToString()); | |
} | |
public void Write(object output, string mimeType) | |
{ | |
string json; | |
if (output is ExpandoObject || output is List<ExpandoObject>) | |
{ | |
if (output is ExpandoObject) json = SerializeExpando(output); | |
else json = "[" + ((List<ExpandoObject>) output) | |
.Select(SerializeExpando) | |
.Aggregate((a, i) => a + ", " + i) + "]"; | |
} | |
else json = _jsonSerializer.Serialize(output); | |
_outputWriter.Write(mimeType, json); | |
} | |
private string SerializeExpando(object expando) | |
{ | |
return "{" + (expando as IDictionary<string, object>) | |
.Select(x => string.Format("\"{0}\": {1}", x.Key, _jsonSerializer.Serialize(x.Value))) | |
.Aggregate((a, i) => a + ", " + i) + "}"; | |
} | |
} |
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
public List<ExpandoObject> post_query(QueryRequest request) | |
{ | |
var results = new List<ExpandoObject>(); | |
using (var connection = new OdbcConnection("DSN=QuickBooks Data")) | |
{ | |
connection.Open(); | |
using (var reader = new OdbcCommand(request.query, connection).ExecuteReader()) | |
{ | |
while (reader.Read()) | |
{ | |
var result = new ExpandoObject() as IDictionary<string, object>; | |
if (reader.FieldCount > 0) | |
for (var i = 0; i < reader.FieldCount; i++) | |
result[reader.GetName(i)] = reader[i]; | |
results.Add((ExpandoObject)result); | |
} | |
} | |
} | |
return results; | |
} |
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
{ query: 'SELECT CompanyName, Contact, Phone FROM Customer' } |
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
[ | |
{ "CompanyName": "Yada", "Contact": "Yada", "Phone": "Yada" }, | |
{ "CompanyName": "Yada", "Contact": "Yada", "Phone": "Yada" } | |
... | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment