Esempio n. 1
0
        private string Serialize(SerializeMethod method, object value)
        {
            string json;

            switch (method)
            {
            case SerializeMethod.JsonNet:
                json = JsonConvert.SerializeObject(value);
                break;

            case SerializeMethod.JsonNetWithIsoConverter:
                json = JsonConvert.SerializeObject(value, new IsoDateTimeConverter());
                break;

            case SerializeMethod.JsonNetLinq:
            {
                TestClass c = value as TestClass;
                if (c != null)
                {
                    JObject o = new JObject(
                        new JProperty("strings", new JArray(
                                          c.strings
                                          )),
                        new JProperty("dictionary", new JObject(c.dictionary.Select(d => new JProperty(d.Key, d.Value)))),
                        new JProperty("Name", c.Name),
                        new JProperty("Now", c.Now),
                        new JProperty("BigNumber", c.BigNumber),
                        new JProperty("Address1", new JObject(
                                          new JProperty("Street", c.Address1.Street),
                                          new JProperty("Phone", c.Address1.Phone),
                                          new JProperty("Entered", c.Address1.Entered))),
                        new JProperty("Addresses", new JArray(c.Addresses.Select(a =>
                                                                                 new JObject(
                                                                                     new JProperty("Street", a.Street),
                                                                                     new JProperty("Phone", a.Phone),
                                                                                     new JProperty("Entered", a.Entered)))))
                        );

                    json = o.ToString(Formatting.None);
                }
                else
                {
                    json = string.Empty;
                }
                break;
            }

            case SerializeMethod.JsonNetManual:
            {
                TestClass c = value as TestClass;
                if (c != null)
                {
                    StringWriter   sw     = new StringWriter();
                    JsonTextWriter writer = new JsonTextWriter(sw);
                    writer.WriteStartObject();
                    writer.WritePropertyName("strings");
                    writer.WriteStartArray();
                    foreach (string s in c.strings)
                    {
                        writer.WriteValue(s);
                    }
                    writer.WriteEndArray();
                    writer.WritePropertyName("dictionary");
                    writer.WriteStartObject();
                    foreach (KeyValuePair <string, int> keyValuePair in c.dictionary)
                    {
                        writer.WritePropertyName(keyValuePair.Key);
                        writer.WriteValue(keyValuePair.Value);
                    }
                    writer.WriteEndObject();
                    writer.WritePropertyName("Name");
                    writer.WriteValue(c.Name);
                    writer.WritePropertyName("Now");
                    writer.WriteValue(c.Now);
                    writer.WritePropertyName("BigNumber");
                    writer.WriteValue(c.BigNumber);
                    writer.WritePropertyName("Address1");
                    writer.WriteStartObject();
                    writer.WritePropertyName("Street");
                    writer.WriteValue(c.BigNumber);
                    writer.WritePropertyName("Street");
                    writer.WriteValue(c.BigNumber);
                    writer.WritePropertyName("Street");
                    writer.WriteValue(c.BigNumber);
                    writer.WriteEndObject();
                    writer.WritePropertyName("Addresses");
                    writer.WriteStartArray();
                    foreach (Address address in c.Addresses)
                    {
                        writer.WriteStartObject();
                        writer.WritePropertyName("Street");
                        writer.WriteValue(address.Street);
                        writer.WritePropertyName("Phone");
                        writer.WriteValue(address.Phone);
                        writer.WritePropertyName("Entered");
                        writer.WriteValue(address.Entered);
                        writer.WriteEndObject();
                    }
                    writer.WriteEndArray();
                    writer.WriteEndObject();

                    writer.Flush();
                    json = sw.ToString();
                }
                else
                {
                    json = string.Empty;
                }
                break;
            }

            case SerializeMethod.JsonNetBinary:
            {
                MemoryStream   ms         = new MemoryStream(Buffer);
                JsonSerializer serializer = new JsonSerializer();
                BsonWriter     writer     = new BsonWriter(ms);
                serializer.Serialize(writer, value);

                //json = BitConverter.ToString(ms.ToArray(), 0, (int)ms.Position);
                json = "Bytes = " + ms.Position;
                break;
            }

            case SerializeMethod.JavaScriptSerializer:
                json = SerializeWebExtensions(value);
                break;

            case SerializeMethod.DataContractJsonSerializer:
                json = SerializeDataContractJson(value);
                break;

            case SerializeMethod.DataContractSerializer:
                json = SerializeDataContract(value);
                break;

            case SerializeMethod.BinaryFormatter:
                json = SerializeBinaryFormatter(value);
                break;

            default:
                throw new ArgumentOutOfRangeException("method");
            }

            return(json);
        }
Esempio n. 2
0
        public void Serialize()
        {
            TestClass test = CreateSerializationObject();

            SerializeTests(test);
        }