Exemple #1
0
        private static void ValueToJson(StringBuilder sb, DynValue value)
        {
            switch (value.Type)
            {
            case DataType.Boolean:
                sb.Append(value.Boolean ? "true" : "false");
                break;

            case DataType.Number:
                sb.Append(value.Number.ToString("r"));
                break;

            case DataType.String:
                sb.Append(EscapeString(value.String ?? ""));
                break;

            case DataType.Table:
                TableToJson(sb, value.Table);
                break;

            case DataType.UserData:
                sb.Append(JsonEmptyArray.IsJsonEmptyArray(value) ? "[]" : "null");
                break;

            case DataType.Nil:
            case DataType.Void:
            default:
                sb.Append("null");
                break;
            }
        }
Exemple #2
0
 private static bool IsValueJsonCompatible(DynValue value)
 {
     return(value.Type == DataType.Boolean || value.IsNil() ||
            value.Type == DataType.Number || value.Type == DataType.String ||
            value.Type == DataType.Table ||
            JsonNull.IsJsonNull(value) ||
            JsonEmptyArray.IsJsonEmptyArray(value));
 }
Exemple #3
0
        private static DynValue ParseJsonArray(Lexer L, Script script, bool emptyArray)
        {
            Table t = new Table(script);

            L.Next();

            while (L.Current.Type != TokenType.Brk_Close_Square)
            {
                DynValue v = ParseJsonValue(L, script, emptyArray);
                t.Append(v);
                L.Next();

                if (L.Current.Type == TokenType.Comma)
                {
                    L.Next();
                }
            }

            return(emptyArray && t.Length == 0 ? JsonEmptyArray.Create() : DynValue.NewTable(t));
        }
Exemple #4
0
        /// <summary>
        /// Converts a generic object to JSON
        /// </summary>
        public static string ObjectToJson(object obj)
        {
            DynValue      v  = ObjectValueConverter.SerializeObjectToDynValue(null, obj, JsonNull.Create(), JsonEmptyArray.Create());
            StringBuilder sb = new StringBuilder();

            ValueToJson(sb, v);
            return(sb.ToString());
        }