I see no problem to use standard DataContractJsonSerializer
to read the data with "$" field. Much more problem I see that you data is not yes in JSON format. If you mean that the data from the question is only an object which will be serialized then the serialized JSON data will be looks like following
{
"message": [
{
"type": "message",
"href": "\/messages\/id\/23",
"view_href": "\/this-is-a-test-message\/m-p\/23",
"id": {
"type": "int",
"$": 23
}
}
]
}
or
var json = '{"message":[{"type":"message","href":"\/messages\/id\/23","view_href":"\/this-is-a-test-message\/m-p\/23","id":{"type":"int","$":23}}]}'
All properties will be double quoted and slash will be escaped (see http://www.json.org/). You can verify on the JSON Validator, that the data are correct JSON data.
To read the data (deserialize) or write (serialize) with respect of DataContractJsonSerializer
you can without any problem. Like I mention before you should only use Name
properties of DataMember
attribute:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using System.IO;
namespace DataContractJsonSerializer4 {
[DataContract]
public class MyId {
[DataMember (Order = 0)]
public string type;
[DataMember (Order = 1, Name="$")]
public int value;
}
[DataContract]
public class Message {
[DataMember (Order = 0)]
public string type { get; set; }
[DataMember (Order = 1)]
public string href { get; set; }
[DataMember (Order = 2)]
public string view_href { get; set; }
[DataMember (Order = 3)]
public MyId id { get; set; }
}
[DataContract]
public class MessageCollection {
[DataMember]
public List<Message> message { get; set; }
}
class Program {
static void Main (string[] args) {
MessageCollection mc = new MessageCollection () {
message = new List<Message> () {
new Message() {
type = "message",
href = "/messages/id/23",
view_href = "/this-is-a-test-message/m-p/23",
id = new MyId() { type="int", value=23}
},
new Message() {
type = "message",
href = "/messages/id/24",
view_href = "/this-is-a-test-message/m-p/24",
id = new MyId() { type="int", value=24}
}
}
};
string json =
"{" +
"\"message\": [" +
"{" +
"\"type\": \"message\"," +
"\"href\": \"\\/messages\\/id\\/23\"," +
"\"view_href\": \"\\/this-is-a-test-message\\/m-p\\/23\"," +
"\"id\": {" +
"\"type\": \"int\"," +
"\"$\": 23" +
"}" +
"}," +
"{" +
"\"type\": \"message\"," +
"\"href\": \"\\/messages\\/id\\/24\"," +
"\"view_href\": \"\\/this-is-a-test-message\\/m-p\\/24\"," +
"\"id\": {" +
"\"type\": \"int\"," +
"\"$\": 24" +
"}" +
"}" +
"]" +
"}";
DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (MessageCollection));
using (MemoryStream ms = new MemoryStream (Encoding.Unicode.GetBytes (json))) {
MessageCollection obj = ser.ReadObject (ms) as MessageCollection;
Console.WriteLine ("JSON data can be read. The value of the fist $ field is {0}", obj.message[0].id.value);
using (MemoryStream ms2 = new MemoryStream ()) {
ser.WriteObject (ms2, obj);
string serializedJson = Encoding.UTF8.GetString (ms2.GetBuffer (), 0, (int)ms2.Length);
Console.WriteLine (serializedJson);
}
}
using (MemoryStream memoryStream = new MemoryStream ()) {
memoryStream.Position = 0;
ser.WriteObject (memoryStream, mc);
memoryStream.Flush ();
memoryStream.Position = 0;
StreamReader sr = new StreamReader (memoryStream);
string str = sr.ReadToEnd ();
Console.WriteLine ("The result of custom serialization:");
Console.WriteLine (str);
}
}
}
}
The program produce following output
JSON data can be read. The value of the fist $ field is 23
{"message":[{"type":"message","href":"\/messages\/id\/23","view_href":"\/this-is-a-test-message\/m-p\/23","id":{"type":"int","$":23}},{"type":"message","href":"\/messages\/id\/24","view_href":"\/this-is-a-test-message\/m-p\/24","id":{"type":"int","$":24}}]}
The result of custom serialization:
{"message":[{"type":"message","href":"\/messages\/id\/23","view_href":"\/this-is-a-test-message\/m-p\/23","id":{"type":"int","$":23}},{"type":"message","href":"\/messages\/id\/24","view_href":"\/this-is-a-test-message\/m-p\/24","id":{"type":"int","$":24}}]}