public void WriteEnum(TextWriter writer, object enumValue)
 {
     if (enumValue == null)
     {
         return;
     }
     if (JsConfig.TreatEnumAsInteger)
     {
         JsWriter.WriteEnumFlags(writer, enumValue);
     }
     else
     {
         writer.Write(enumValue.ToString());
     }
 }
Example #2
0
 public static string EncodeJsv(this string value)
 {
     if (JsState.QueryStringMode)
     {
         return(UrlEncode(value));
     }
     return(String.IsNullOrEmpty(value) || !JsWriter.HasAnyEscapeChars(value)
         ? value
         : String.Concat
            (
                JsWriter.QuoteString,
                value.Replace(JsWriter.QuoteString, TypeSerializer.DoubleQuoteString),
                JsWriter.QuoteString
            ));
 }
Example #3
0
 public void WriteEnum(TextWriter writer, object enumValue)
 {
     if (enumValue == null)
     {
         return;
     }
     if (GetTypeInfo(enumValue.GetType()).IsNumeric)
     {
         JsWriter.WriteEnumFlags(writer, enumValue);
     }
     else
     {
         WriteRawString(writer, enumValue.ToString());
     }
 }
Example #4
0
    /// <summary>
    /// Writes the string array.
    /// </summary>
    /// <param name="serializer">The serializer.</param>
    /// <param name="writer">The writer.</param>
    /// <param name="oList">The o list.</param>
    public static void WriteStringArray(ITypeSerializer serializer, TextWriter writer, object oList)
    {
        writer.Write(JsWriter.ListStartChar);

        var list       = (string[])oList;
        var ranOnce    = false;
        var listLength = list.Length;

        for (var i = 0; i < listLength; i++)
        {
            JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce);
            serializer.WriteString(writer, list[i]);
        }

        writer.Write(JsWriter.ListEndChar);
    }
Example #5
0
    /// <summary>
    /// Writes the enum.
    /// </summary>
    /// <param name="writer">The writer.</param>
    /// <param name="enumValue">The enum value.</param>
    public void WriteEnum(TextWriter writer, object enumValue)
    {
        if (enumValue == null)
        {
            return;
        }
        var serializedValue = CachedTypeInfo.Get(enumValue.GetType()).EnumInfo.GetSerializedValue(enumValue);

        if (serializedValue is string strEnum)
        {
            WriteRawString(writer, strEnum);
        }
        else
        {
            JsWriter.WriteEnumFlags(writer, enumValue);
        }
    }
Example #6
0
        public static void Write(TextWriter writer, IEnumerable <List <string> > rows)
        {
            if (Headers.Count > 0)
            {
                var ranOnce = false;
                foreach (var header in Headers)
                {
                    JsWriter.WriteItemSeperatorIfRanOnce(writer, ref ranOnce);

                    writer.Write(header);
                }
                writer.WriteLine();
            }

            foreach (var row in rows)
            {
                WriteRow(writer, row);
            }
        }
 public void WriteEnumFlags(TextWriter writer, object enumFlagValue)
 {
     JsWriter.WriteEnumFlags(writer, enumFlagValue);
 }
 static QueryStringSerializer()
 {
     JsConfig.InitStatics();
     Instance = new JsWriter <JsvTypeSerializer>();
 }
Example #9
0
 static JsonAotConfig()
 {
     serializer = new JsonTypeSerializer();
     reader     = new JsReader <JsonTypeSerializer>();
     writer     = new JsWriter <JsonTypeSerializer>();
 }
Example #10
0
 public override void Write(JsWriter writer)
 {
     writer.WriteValues(_values, ", ");
 }
Example #11
0
 public override void Write(JsWriter writer)
 {
     //TODO: if not defined
     writer.Write("{0} = {{}};", Name);
 }
Example #12
0
    /// <summary>
    /// Strings to type.
    /// </summary>
    /// <param name="strType">Type of the string.</param>
    /// <param name="typeConfig">The type configuration.</param>
    /// <param name="ctorFn">The ctor function.</param>
    /// <param name="typeAccessors">The type accessors.</param>
    /// <returns>System.Object.</returns>
    internal static object StringToType(ReadOnlySpan <char> strType,
                                        TypeConfig typeConfig,
                                        EmptyCtorDelegate ctorFn,
                                        KeyValuePair <string, TypeAccessor>[] typeAccessors)
    {
        var index = 0;
        var type  = typeConfig.Type;

        if (strType.IsEmpty)
        {
            return(null);
        }

        //if (!Serializer.EatMapStartChar(strType, ref index))
        if (strType[index++] != JsWriter.MapStartChar)
        {
            throw DeserializeTypeRef.CreateSerializationError(type, strType.ToString());
        }

        if (JsonTypeSerializer.IsEmptyMap(strType))
        {
            return(ctorFn());
        }

        var config = JsConfig.GetConfig();

        object instance = null;
        var    lenient  = config.PropertyConvention == PropertyConvention.Lenient || config.TextCase == TextCase.SnakeCase;

        var strTypeLength = strType.Length;

        while (index < strTypeLength)
        {
            var propertyName = Serializer.EatMapKey(strType, ref index).Trim();

            //Serializer.EatMapKeySeperator(strType, ref index);
            index++;

            var propertyValueStr = Serializer.EatValue(strType, ref index);
            var possibleTypeInfo = propertyValueStr != null && propertyValueStr.Length > 1;

            if (possibleTypeInfo && propertyName.Equals(typeAttr.Span, StringComparison.OrdinalIgnoreCase))
            {
                var explicitTypeName = Serializer.ParseString(propertyValueStr);
                var explicitType     = config.TypeFinder(explicitTypeName);

                if (explicitType == null || explicitType.IsInterface || explicitType.IsAbstract)
                {
                    Tracer.Instance.WriteWarning("Could not find type: " + propertyValueStr.ToString());
                }
                else if (!type.IsAssignableFrom(explicitType))
                {
                    Tracer.Instance.WriteWarning("Could not assign type: " + propertyValueStr.ToString());
                }
                else
                {
                    JsWriter.AssertAllowedRuntimeType(explicitType);
                    instance = explicitType.CreateInstance();
                }

                if (instance != null)
                {
                    //If __type info doesn't match, ignore it.
                    if (!type.IsInstanceOfType(instance))
                    {
                        instance = null;
                    }
                    else
                    {
                        var derivedType = instance.GetType();
                        if (derivedType != type)
                        {
                            var map = DeserializeTypeRef.GetCachedTypeAccessors(derivedType, Serializer);
                            if (map != null)
                            {
                                typeAccessors = map;
                            }
                        }
                    }
                }

                //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
                if (index != strType.Length)
                {
                    index++;
                }

                continue;
            }

            instance ??= ctorFn();

            var typeAccessor = typeAccessors.Get(propertyName, lenient);

            var propType = possibleTypeInfo && propertyValueStr[0] == '_' ? TypeAccessor.ExtractType(Serializer, propertyValueStr) : null;
            if (propType != null)
            {
                try
                {
                    if (typeAccessor != null)
                    {
                        var parseFn       = Serializer.GetParseStringSpanFn(propType);
                        var propertyValue = parseFn(propertyValueStr);
                        if (typeConfig.OnDeserializing != null)
                        {
                            propertyValue = typeConfig.OnDeserializing(instance, propertyName.ToString(), propertyValue);
                        }
                        typeAccessor.SetProperty(instance, propertyValue);
                    }

                    //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
                    if (index != strType.Length)
                    {
                        index++;
                    }

                    continue;
                }
                catch (Exception e)
                {
                    config.OnDeserializationError?.Invoke(instance, propType, propertyName.ToString(), propertyValueStr.ToString(), e);
                    if (config.ThrowOnError)
                    {
                        throw DeserializeTypeRef.GetSerializationException(propertyName.ToString(), propertyValueStr.ToString(), propType, e);
                    }
                    else
                    {
                        Tracer.Instance.WriteWarning("WARN: failed to set dynamic property {0} with: {1}", propertyName.ToString(), propertyValueStr.ToString());
                    }
                }
            }

            if (typeAccessor is { GetProperty : { }, SetProperty : { } })
Example #13
0
 public override void Write(JsWriter writer)
 {
     writer.Write("undefined");
 }
Example #14
0
    /// <summary>
    /// Strings to type.
    /// </summary>
    /// <param name="strType">Type of the string.</param>
    /// <param name="typeConfig">The type configuration.</param>
    /// <param name="ctorFn">The ctor function.</param>
    /// <param name="typeAccessors">The type accessors.</param>
    /// <returns>System.Object.</returns>
    internal static object StringToType(ReadOnlySpan <char> strType,
                                        TypeConfig typeConfig,
                                        EmptyCtorDelegate ctorFn,
                                        KeyValuePair <string, TypeAccessor>[] typeAccessors)
    {
        var index = 0;
        var type  = typeConfig.Type;

        if (strType.IsEmpty)
        {
            return(null);
        }

        var buffer        = strType;
        var strTypeLength = strType.Length;

        //if (!Serializer.EatMapStartChar(strType, ref index))
        for (; index < strTypeLength; index++)
        {
            if (!JsonUtils.IsWhiteSpace(buffer[index]))
            {
                break;
            }
        }                                                                                             //Whitespace inline
        if (buffer[index] != JsWriter.MapStartChar)
        {
            throw DeserializeTypeRef.CreateSerializationError(type, strType.ToString());
        }

        index++;
        if (JsonTypeSerializer.IsEmptyMap(strType, index))
        {
            return(ctorFn());
        }

        var config   = JsConfig.GetConfig();
        var typeAttr = config.TypeAttrMemory;

        object instance = null;
        var    textCase = typeConfig.TextCase.GetValueOrDefault(config.TextCase);
        var    lenient  = config.PropertyConvention == PropertyConvention.Lenient || textCase == TextCase.SnakeCase;

        for (; index < strTypeLength; index++)
        {
            if (!JsonUtils.IsWhiteSpace(buffer[index]))
            {
                break;
            }
        }                                                                                             //Whitespace inline

        while (index < strTypeLength)
        {
            var propertyName = JsonTypeSerializer.UnescapeJsString(strType, JsonUtils.QuoteChar, true, ref index);

            //Serializer.EatMapKeySeperator(strType, ref index);
            for (; index < strTypeLength; index++)
            {
                if (!JsonUtils.IsWhiteSpace(buffer[index]))
                {
                    break;
                }
            }                                                                                             //Whitespace inline
            if (strTypeLength != index)
            {
                index++;
            }

            var propertyValueStr = Serializer.EatValue(strType, ref index);
            var possibleTypeInfo = propertyValueStr != null && propertyValueStr.Length > 1;

            switch (instance)
            {
            //if we already have an instance don't check type info, because then we will have a half deserialized object
            //we could throw here or just use the existing instance.
            case null when possibleTypeInfo && propertyName.Equals(typeAttr.Span, StringComparison.OrdinalIgnoreCase):
            {
                var explicitTypeName = Serializer.ParseString(propertyValueStr);
                var explicitType     = config.TypeFinder(explicitTypeName);

                if (explicitType == null || explicitType.IsInterface || explicitType.IsAbstract)
                {
                    Tracer.Instance.WriteWarning("Could not find type: " + propertyValueStr.ToString());
                }
                else if (!type.IsAssignableFrom(explicitType))
                {
                    Tracer.Instance.WriteWarning("Could not assign type: " + propertyValueStr.ToString());
                }
                else
                {
                    JsWriter.AssertAllowedRuntimeType(explicitType);
                    instance = explicitType.CreateInstance();
                }

                if (instance != null)
                {
                    //If __type info doesn't match, ignore it.
                    if (!type.IsInstanceOfType(instance))
                    {
                        instance = null;
                    }
                    else
                    {
                        var derivedType = instance.GetType();
                        if (derivedType != type)
                        {
                            var map = DeserializeTypeRef.GetCachedTypeAccessors(derivedType, Serializer);
                            if (map != null)
                            {
                                typeAccessors = map;
                            }
                        }
                    }
                }

                Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
                continue;
            }

            case null:
                instance = ctorFn();
                break;
            }

            var typeAccessor = typeAccessors.Get(propertyName, lenient);

            var propType = possibleTypeInfo && propertyValueStr[0] == '_' ? TypeAccessor.ExtractType(Serializer, propertyValueStr) : null;
            if (propType != null)
            {
                try
                {
                    if (typeAccessor != null)
                    {
                        //var parseFn = Serializer.GetParseFn(propType);
                        var parseFn = JsonReader.GetParseStringSpanFn(propType);

                        var propertyValue = parseFn(propertyValueStr);
                        if (typeConfig.OnDeserializing != null)
                        {
                            propertyValue = typeConfig.OnDeserializing(instance, propertyName.ToString(), propertyValue);
                        }
                        typeAccessor.SetProperty(instance, propertyValue);
                    }

                    //Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
                    for (; index < strTypeLength; index++)
                    {
                        if (!JsonUtils.IsWhiteSpace(buffer[index]))
                        {
                            break;
                        }
                    }                                                                                             //Whitespace inline
                    if (index != strTypeLength)
                    {
                        var success = buffer[index] == JsWriter.ItemSeperator || buffer[index] == JsWriter.MapEndChar;
                        index++;
                        if (success)
                        {
                            for (; index < strTypeLength; index++)
                            {
                                if (!JsonUtils.IsWhiteSpace(buffer[index]))
                                {
                                    break;
                                }
                            }                                                                                             //Whitespace inline
                        }
                    }

                    continue;
                }
                catch (Exception e)
                {
                    config.OnDeserializationError?.Invoke(instance, propType, propertyName.ToString(), propertyValueStr.ToString(), e);
                    if (config.ThrowOnError)
                    {
                        throw DeserializeTypeRef.GetSerializationException(propertyName.ToString(), propertyValueStr.ToString(), propType, e);
                    }
                    else
                    {
                        Tracer.Instance.WriteWarning("WARN: failed to set dynamic property {0} with: {1}", propertyName.ToString(), propertyValueStr.ToString());
                    }
                }
            }

            if (typeAccessor is { GetProperty : { }, SetProperty : { } })