Example #1
0
        /// <summary>
        /// Serializes an object to IJsonValue
        /// </summary>
        /// <param name="value">object to serialize</param>
        /// <returns>IJsonValue representation of the object</returns>
        public static IJsonValue SerializeAsJsonValue(object value)
        {
            IJsonValue serializedValue = Singleton<JsonNull>.Instance;

            if (value == null)
                return serializedValue;

            var type = value.GetType();

            if (!SerializationHelper.IsIgnore(type))
            {
                if (type.IsPrimitive)
                    serializedValue = SerializePrimitive(value, type);
                else if (Type.GetTypeCode(type) == TypeCode.String)
                    serializedValue = new JsonString(value as string);
                else if (value is System.Collections.IEnumerable)
                    serializedValue = SerializeEnumerable(value);
                else if (value is DateTime)
                    serializedValue = SerializeDateTime((DateTime)value);
                else if (type.IsClass || type.IsValueType)
                    serializedValue = SerializeClassOrValueType(value, type);
                //else not supported
            }

            return serializedValue;
        }
Example #2
0
        public void JsonStrings()
        {
            var jsonString = new JsonString();

            Log.Info("Testing default value = string.Empty");

            Assert.That(jsonString.Value != null);
            Assert.That(string.Empty == jsonString);

            foreach (var testString in TestStrings)
            {
                Log.Info("Setting value = {0}", testString);
                jsonString = testString;

                Log.Info("Testing string => {0}", jsonString);
                Assert.That(jsonString.Value.Length, Is.GreaterThan(0));
            }
        }