Esempio n. 1
0
        public static string ToJsonString(this IJsonSerializable jsonSerializable)
        {
            if (ReferenceEquals(jsonSerializable, null))
            {
                return(string.Empty);
            }

            var stringBuilder = new StringBuilder();

            using (var writer = JSON.GetWriter(new StringWriter(stringBuilder)))
            {
                try
                {
                    jsonSerializable.ToJson(writer);
                }
                catch (Exception ex)
                {
                    if (Log != null)
                    {
                        Log.WarnFormat(
                            "Unable to serialize component: {0} - {1}",
                            jsonSerializable.GetType().Name,
                            ex.Message
                            );
                    }
                    throw;
                }
            }

            return(stringBuilder.ToString());
        }
    public static void Serialize(this IJsonSerializable serializable, Dictionary <string, object> json)
    {
        if (serializable == null)
        {
            return;
        }
        serializable.GetType().GetJsonSerializedFields().ForEach(f => {
            json.AddOrSet(f.Name, SerializeValue(f.GetValue(serializable)));
        });

        if (serializable is IJsonSerializableCustom)
        {
            ((IJsonSerializableCustom)serializable).SerializeCustom(json);
        }
    }