Skip to content

Instantly share code, notes, and snippets.

@JohnMurray
Created June 1, 2012 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohnMurray/2848335 to your computer and use it in GitHub Desktop.
Save JohnMurray/2848335 to your computer and use it in GitHub Desktop.
Code descriptions for log-entry on API Anti-Patterns (johnmurray.io)
{
"user_id": 1,
"car_id": 2,
"template_data": [
{"Key": "key1", "Value": "value1"},
{"Key": "key2", "Value": "value2"},
{"Key": "key3", "Value": "value3"}
],
"key": [
{"Key": "key1", "Value": "value1"},
{"Key": "key2", "Value": "value2"}
]
}
public static class IDictionaryExtension
{
public static JsonDictionary<K, V> ToJsonDictionary<K,V>(this IDictionary<K, V> dict)
{
if (dict == null)
return null;
var jsonDict = new JsonDictionary<K, V>();
foreach (var key in dict.Keys)
{
jsonDict[key] = dict[key];
}
return jsonDict;
}
}
uri = URI.parse('http://localhost:4567')
Net::HTTP.start(uri.host, uri.port) do |http|
headers = {'Content-Type': 'application/json'}
http.post2('/service_path', data.to_json, headers) do |resp|
return resp.code == '200'
end
end
var dict = new Dictionary<String, Int32>
{
{"one", 1 },
{"two", 2 },
{"three", 3 },
// ...
{"ten", 10}
};
{
"user_id": 1,
"car_id": 2,
"template_data": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
},
"key": {
"key1": "value1",
"key2": "value2"
}
}
/// <summary>
/// Taken from: http://goo.gl/Xafe5
/// </summary>
[Serializable]
public class JsonDictionary<K, V> : ISerializable
{
Dictionary<K, V> dict = new Dictionary<K, V>();
public JsonDictionary() { }
protected JsonDictionary(SerializationInfo info, StreamingContext context)
{
throw new NotImplementedException();
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
foreach (K key in dict.Keys)
{
info.AddValue(key.ToString(), dict[key]);
}
}
public void Add(K key, V value)
{
dict.Add(key, value);
}
public V this[K index]
{
set { dict[index] = value; }
get { return dict[index]; }
}
}
[DataContract]
public class MyObjectDataContract
{
[DataMember(Name = "user_id")]
public int? UserId { get; set; }
[DataMember(Name = "car_id")]
public int? CarId { get; set; }
[DataMember(Name = "template_data")]
public Dictionary<String, object> TemplateData { get; set; }
[DataMember(Name = "key")]
public Dictionary<String, object> Key { get; set; }
}
WebRequest request = WebRequest.Create(NSUrl);
request.Method = "POST";
request.ContentLength = jsonStream.Length;
request.ContentType = "application/json";
Stream requestStream = request.GetRequestStream();
requestStream.Write(jsonStream.ToArray(), 0, (int)jsonStream.Length);
requestStream.Close();
// Build up request
// ...
// Send request
request.GetResponse();
// assume myObjectDataContract is already set and defined
MemoryStream jsonStream = new MemoryStream();
DataContractJsonSerializer ser = new DataContractJsonSerializer(
typeof(MyObjectDataContract));
ser.WriteObject(jsonStream, myObjectDataContract);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment