[mono-android] Being driven insane....

Mike Child mike at mikechild.net
Wed Nov 23 12:02:06 EST 2011


I found this code through some searching. I am using json in my code
but using xml should be relatively simple change in the code.

public static byte[] ToJson<T>(T instance)  --> it's converted to
byte[] because i'm using it in httpwebrequest, you could just return
the string.
    {
      DataContractJsonSerializer ser = new
DataContractJsonSerializer(typeof(T));
      MemoryStream ms = new MemoryStream();
      ser.WriteObject (ms,instance);
      String json = Encoding.UTF8.GetString (ms.ToArray ());
      return UTF8Encoding.UTF8.GetBytes (json);
    }

    public static T FromJson<T>(string json)
    {

      DataContractJsonSerializer ser = new
DataContractJsonSerializer(typeof(T));
      using(var tempStream = new MemoryStream(Encoding.UTF8.GetBytes (json)))
        return (T)ser.ReadObject(tempStream);

    }

Mike Child


On Wed, Nov 23, 2011 at 11:16 AM, nodoid <paul at all-the-johnsons.co.uk> wrote:
>
> Hi,
>
> Re XML serialization...
>
> Typically, I would use (say)
>
> foo p = new p();
>
> System.Xml.Serialization.XmlSerializer x = new
> System.Xml.Serialization.XmlSerializer(p.GetType());
> x.Serialize(Console.Out, p);
>
> Obviously, I can't use Console.Out, so where do I need to point it to (for
> example a temporary file or something like that)?
>
> Thanks
>
> Paul
>
> --
> View this message in context: http://mono-for-android.1047100.n5.nabble.com/Being-driven-insane-tp5016975p5017190.html
> Sent from the Mono for Android mailing list archive at Nabble.com.
> _______________________________________________
> Monodroid mailing list
> Monodroid at lists.ximian.com
>
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid


More information about the Monodroid mailing list