This project has moved and is read-only. For the latest updates, please go here.

Serializing objects with JSON object root

Jul 29, 2009 at 3:11 PM

I have a class like

    class WebServletDeviceInformation
        [JsonProperty(PropertyName = "IMSI")]
        public string Imsi { get; set; }

        [JsonProperty(PropertyName = "IMEI")]
        public string Imei { get; set; }

which i want to serialize to JSON like:


How can i get the deviceinformation root object there without wrapping the object myself in a single property class (kinda stupid) or writing something like:

      var serializer = new JsonSerializer();
      using (var json = new StringWriter())
          using (JsonWriter jsonWriter = new JsonTextWriter(json))
              serializer.Serialize(jsonWriter, obj);
              return json.ToString();
Jul 30, 2009 at 1:19 AM

You can't. Your solution looks pretty good to me.