コード例 #1
0
        /// <summary>
        /// BSON 포맷에서 직렬화를 해제한다.
        /// </summary>
        /// <param name="reader">BSON 데이터를 읽어올 수 있는 BinaryReader 객체</param>
        public static BlockarObject DeserializeFromBson(BinaryReader reader)
        {
            var obj = new BlockarObject();

            __BsonParseBsonObject(obj, reader);
            return(obj);
        }
コード例 #2
0
 /// <summary>
 /// JSON 포맷으로 직렬화한다.
 /// </summary>
 /// <param name="writer">직렬화한 데이터를 보관할 TextWriter 객체</param>
 /// <param name="obj">직렬화할 데이터</param>
 public static void SerializeToBson(BinaryWriter writer, BlockarObject obj)
 {
     writer.Write(0);
     foreach (var innerObj in obj.objs)
     {
         __BsonObjectToWriter(writer, innerObj.Key, innerObj.Value);
     }
     writer.Flush();
 }
コード例 #3
0
        /// <summary>
        /// JSON 포맷으로 직렬화한다.
        /// </summary>
        /// <param name="stream">직렬화한 데이터를 보관할 Stream 객체</param>
        public static void SerializeToJson(Stream stream, BlockarObject obj)
        {
#if NET20 || NET35
            using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
#else
            using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8, 4096, true))
#endif
            {
                SerializeToJson(writer, obj);
            }
        }
コード例 #4
0
        public static IEnumerable <BlockarObject> DeserializeFromIni(TextReader reader)
        {
            BlockarObject obj = new BlockarObject();

            while (true)
            {
                int    i    = 0;
                string line = reader.ReadLine();
                if (line == null)
                {
                    break;
                }
                if (line.Length == 0)
                {
                    continue;
                }
                for (; i < line.Length; ++i)
                {
                    char ch = line [i];
                    if (ch != ' ' && ch != '\t' && ch != '\a' && ch != '\r')
                    {
                        break;
                    }
                }
                switch (line [i])
                {
                case ';':
                    continue;

                case '[':
                {
                    if (obj.Count > 0)
                    {
                        yield return(obj);
                    }
                    obj = new BlockarObject {
                        SectionName = __IniGetSectionTitle(line, i + 1)
                    };
                    break;
                }

                default:
                {
                    var key   = __IniGetKey(line, ref i);
                    var value = __IniGetValue(line, i);
                    obj.Set(key, value);
                    break;
                }
                }
            }
            yield return(obj);
        }
コード例 #5
0
        /// <summary>
        /// JSON 포맷으로 직렬화한다.
        /// </summary>
        /// <param name="writer">직렬화한 데이터를 보관할 TextWriter 객체</param>
        /// <param name="obj">직렬화할 데이터</param>
        public static void SerializeToJson(TextWriter writer, BlockarObject obj)
        {
            writer.Write('{');
            foreach (var innerObj in obj.objs)
            {
                writer.Write($"\"{innerObj.Key}\":");
                __JsonObjectToWriter(writer, innerObj.Value);

                if (innerObj != obj.objs[obj.objs.Count - 1])
                {
                    writer.Write(',');
                }
            }

            writer.Write('}');
            writer.Flush();
        }
コード例 #6
0
        /// <summary>
        /// JSON 포맷에서 직렬화를 해제한다.
        /// </summary>
        /// <param name="reader">JSON 데이터를 읽어올 수 있는 TextReader 객체</param>
        public static BlockarObject DeserializeFromJson(TextReader reader)
        {
            BlockarObject obj = new BlockarObject();
            char          rc  = __JsonPassWhitespaces(reader);

            if (rc == 0xffff)
            {
                return(obj);
            }

            if (rc == '{')
            {
                StringBuilder stringBuilder = new StringBuilder(4096);
                char[]        charBuffer    = new char [6];
                __JsonInnerDeserializeObjectFromJson(obj, reader, stringBuilder, charBuffer);
            }
            else
            {
                throw new BlockarJsonDeserializeException();
            }

            return(obj);
        }
コード例 #7
0
        static void InnerDeserializeArrayFromJson(List <object> arr, TextReader reader, StringBuilder stringBuilder,
                                                  char[] charBuffer)
        {
            try
            {
                JsonParseState parseState = JsonParseState.None;

                while (true)
                {
#if !(NET20 || NET35)
                    stringBuilder.Clear();
#else
                    stringBuilder.Remove(0, stringBuilder.Length);
#endif

                    char rc = __JsonPassWhitespaces(reader);
                    if (rc == ',')
                    {
                        parseState = JsonParseState.None;
                    }
                    else if (rc == '"')
                    {
                        if (parseState != JsonParseState.None)
                        {
                            throw new BlockarJsonDeserializeException();
                        }
                        __JsonGetStringFromString(reader, stringBuilder, charBuffer);
                        if (arr.Count == arr.Capacity)
                        {
                            arr.Capacity *= 2;
                        }
                        arr.Add(stringBuilder.ToString());
                        parseState = JsonParseState.Value;
                    }
                    else if (rc == 't' || rc == 'f' || rc == 'n')
                    {
                        if (parseState != JsonParseState.None)
                        {
                            throw new BlockarJsonDeserializeException();
                        }
                        if (arr.Count == arr.Capacity)
                        {
                            arr.Capacity *= 2;
                        }
                        arr.Add(__JsonGetKeywordFromString(reader, rc, charBuffer));
                        parseState = JsonParseState.Value;
                    }
                    else if ((rc >= '0' && rc <= '9') || rc == '-' || rc == '+')
                    {
                        if (parseState != JsonParseState.None)
                        {
                            throw new BlockarJsonDeserializeException();
                        }
                        if (arr.Count == arr.Capacity)
                        {
                            arr.Capacity *= 2;
                        }
                        arr.Add(__JsonGetNumberFromString(reader, rc, stringBuilder));
                        parseState = JsonParseState.Value;
                    }
                    else if (rc == '{')
                    {
                        if (parseState != JsonParseState.None)
                        {
                            throw new BlockarJsonDeserializeException();
                        }
                        BlockarObject inner = new BlockarObject();
                        __JsonInnerDeserializeObjectFromJson(inner, reader, stringBuilder, charBuffer);
                        if (arr.Count == arr.Capacity)
                        {
                            arr.Capacity *= 2;
                        }
                        arr.Add(inner);
                        parseState = JsonParseState.Value;
                    }
                    else if (rc == '[')
                    {
                        if (parseState != JsonParseState.None)
                        {
                            throw new BlockarJsonDeserializeException();
                        }
                        List <object> inner = new List <object>();
                        InnerDeserializeArrayFromJson(inner, reader, stringBuilder, charBuffer);
                        if (arr.Count == arr.Capacity)
                        {
                            arr.Capacity *= 2;
                        }
                        arr.Add(inner);
                        parseState = JsonParseState.Value;
                    }
                    else if (rc == ']')
                    {
                        break;
                    }
                    else
                    {
                        throw new BlockarJsonDeserializeException();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new BlockarJsonDeserializeException(ex);
            }
        }
コード例 #8
0
        static void __JsonInnerDeserializeObjectFromJson(BlockarObject blockarObject, TextReader reader,
                                                         StringBuilder stringBuilder, char[] charBuffer)
        {
            try
            {
                var parseState = JsonParseState.Key;

#if !NET35
                if (!tokenStackQueue.TryDequeue(out var tokenStack))
                {
                    tokenStack = new Queue <object>(128);
                }
#else
                var tokenStack = new Queue <object> (128);
#endif

                while (true)
                {
#if !(NET20 || NET35)
                    stringBuilder.Clear();
#else
                    stringBuilder.Remove(0, stringBuilder.Length);
#endif

                    var rc = __JsonPassWhitespaces(reader);
                    if (rc == ':')
                    {
                        if (parseState != JsonParseState.Key)
                        {
                            throw new BlockarJsonDeserializeException();
                        }
                        parseState = JsonParseState.Value;
                    }
                    else if (rc == ',')
                    {
                        if (parseState != JsonParseState.Value)
                        {
                            throw new BlockarJsonDeserializeException();
                        }
                        parseState = JsonParseState.Key;
                    }
                    else if (rc == '"')
                    {
                        __JsonGetStringFromString(reader, stringBuilder, charBuffer);
                        tokenStack.Enqueue(stringBuilder.ToString());
                    }
                    else if (rc == 't' || rc == 'f' || rc == 'n')
                    {
                        tokenStack.Enqueue(__JsonGetKeywordFromString(reader, rc, charBuffer));
                    }
                    else if ((rc >= '0' && rc <= '9') || rc == '-' || rc == '+')
                    {
                        tokenStack.Enqueue(__JsonGetNumberFromString(reader, rc, stringBuilder));
                    }
                    else if (rc == '{')
                    {
                        var inner = new BlockarObject();
                        __JsonInnerDeserializeObjectFromJson(inner, reader, stringBuilder, charBuffer);
                        tokenStack.Enqueue(inner);
                    }
                    else if (rc == '[')
                    {
                        var inner = new List <object>(16);
                        InnerDeserializeArrayFromJson(inner, reader, stringBuilder, charBuffer);
                        tokenStack.Enqueue(inner);
                    }
                    else if (rc == '}')
                    {
                        break;
                    }
                    else
                    {
                        throw new BlockarJsonDeserializeException();
                    }
                }

                if (tokenStack.Count % 2 != 0)
                {
                    throw new BlockarJsonDeserializeException();
                }

                while (tokenStack.Count != 0)
                {
                    string key   = tokenStack.Dequeue() as string;
                    object value = tokenStack.Dequeue();
                    blockarObject.Set(key, value);
                }

#if !NET35
                tokenStackQueue.Enqueue(tokenStack);
#endif
            }
            catch (Exception ex)
            {
                throw new BlockarJsonDeserializeException(ex);
            }
        }
コード例 #9
0
        public static IEnumerable <BlockarObject> DeserializeFromCsv(TextReader reader, bool requireTitleRow = true, char separator = CsvSeparatorDetectorCharacter)
        {
            List <string> columnNames;

            if (separator == CsvSeparatorDetectorCharacter)
            {
                var columnNameRow = reader.ReadLine();
                if (requireTitleRow)
                {
#if NET20 || NET35
                    if (columnNameRow.IndexOf(',') >= 0)
                    {
                        separator = ',';
                    }
                    else if (columnNameRow.IndexOf('\t') >= 0)
                    {
                        separator = '\t';
                    }
                    else if (columnNameRow.IndexOf('|') >= 0)
                    {
                        separator = '|';
                    }
#else
                    if (columnNameRow.Contains(','))
                    {
                        separator = ',';
                    }
                    else if (columnNameRow.Contains('\t'))
                    {
                        separator = '\t';
                    }
                    else if (columnNameRow.Contains('|'))
                    {
                        separator = '|';
                    }
#endif
                    else
                    {
                        throw new ArgumentException("Unknown Separator.");
                    }
                }
                else
                {
                    throw new ArgumentException("Cannot Separator Detection Non-required Title row.");
                }

                columnNames = new List <string> (columnNameRow.Split(separator));
            }
            else
            {
                columnNames = new List <string> ();
            }

            BlockarObject       obj     = null;
            CsvDeserializeState state   = CsvDeserializeState.StartRow;
            StringBuilder       builder = new StringBuilder();
            int columnNumber            = 0;
            while (true)
            {
                char ch = (char)reader.Peek();
                if (ch == 0xffff)
                {
                    break;
                }

                switch (state)
                {
                case CsvDeserializeState.StartRow:
                {
                    obj          = new BlockarObject();
                    state        = CsvDeserializeState.StartColumn;
                    columnNumber = 0;
                }
                break;

                case CsvDeserializeState.StartColumn:
                {
                    ch = (char)reader.Read();
                    switch (ch)
                    {
                    case '"':
                        state = CsvDeserializeState.WrappedColumning;
                        break;

                    case '\r':
                        continue;

                    case '\n':
                    {
                        if (obj.Count != 0)
                        {
                            yield return(obj);

                            obj = null;
                        }
                        state = CsvDeserializeState.StartRow;
                        break;
                    }

                    default:
                        state = CsvDeserializeState.Columning;
                        builder.Append(ch);
                        break;
                    }
                }
                break;

                case CsvDeserializeState.Columning:
                {
                    if (ch == '\n')
                    {
                        state = CsvDeserializeState.EndColumn;
                    }
                    else
                    {
                        ch = (char)reader.Read();
                        if (ch == separator)
                        {
                            state = CsvDeserializeState.EndColumn;
                        }
                        else
                        {
                            builder.Append(ch);
                        }
                    }
                }
                break;

                case CsvDeserializeState.WrappedColumning:
                {
                    if (builder.Length > 0 && builder [builder.Length - 1] == '"' && ch == '\n')
                    {
                        builder.Remove(builder.Length - 1, 1);
                        state = CsvDeserializeState.EndColumn;
                    }
                    else
                    {
                        ch = (char)reader.Read();
                        builder.Append(ch);
                        if (builder.Length >= 2)
                        {
                            if (builder [builder.Length - 2] == '"' && builder [builder.Length - 1] == separator)
                            {
                                builder.Remove(builder.Length - 2, 2);
                                state = CsvDeserializeState.EndColumn;
                            }
                        }
                    }
                }
                break;

                case CsvDeserializeState.EndColumn:
                {
                    if (!requireTitleRow && columnNames.Count <= columnNumber)
                    {
                        columnNames.Add((columnNumber + 1).ToString());
                    }
                    obj.Set(columnNames [columnNumber], builder.ToString());
#if NET20 || NET35
                    builder = new StringBuilder();
#else
                    builder.Clear();
#endif
                    ++columnNumber;
                    state = CsvDeserializeState.StartColumn;
                }
                break;
                }
            }

            if (obj != null && obj.Count > 0)
            {
                yield return(obj);
            }
        }
コード例 #10
0
        static void __BsonParseBsonArray(List <object> arr, BinaryReader reader)
        {
            try
            {
                bool isParsing       = true;
                int  dataSize        = reader.ReadInt32();
                int  currentPosition = (int)reader.BaseStream.Position;
                while (isParsing && (reader.BaseStream.Position - currentPosition) != dataSize)
                {
                    BSONType rb = (BSONType)reader.ReadByte();
                    if (rb == BSONType.EndDoc)
                    {
                        break;
                    }

                    reader.ReadByte();

                    switch (rb)
                    {
                    case BSONType.EndDoc: isParsing = false; break;

                    case BSONType.Double: arr.Add(reader.ReadDouble()); break;

                    case BSONType.String: arr.Add(__BsonGetStringFromBinary(reader)); break;

                    case BSONType.Document:
                    {
                        BlockarObject inner = new BlockarObject();
                        __BsonParseBsonObject(inner, reader);
                        arr.Add(inner);
                    }
                    break;

                    case BSONType.Array:
                    {
                        List <object> innerArr = new List <object> ();
                        __BsonParseBsonArray(innerArr, reader);
                        arr.Add(innerArr.ToArray());
                    }
                    break;

                    case BSONType.BinaryData: arr.Add(__BsonGetBinaryFromBinary(reader)); break;

                    case BSONType.Boolean: arr.Add(reader.ReadByte() != 0); break;

                    case BSONType.UTCTime: arr.Add(DateTime.FromFileTimeUtc(reader.ReadInt64())); break;

                    case BSONType.Null: arr.Add(null); break;

                    case BSONType.Regexp: arr.Add(new Regex(__BsonGetStringFromBinary(reader))); break;

                    case BSONType.JavascriptCode: arr.Add(__BsonGetStringFromBinary(reader)); break;

                    case BSONType.Integer: arr.Add(reader.ReadInt32()); break;

                    case BSONType.Integer64: arr.Add(reader.ReadInt64()); break;

                    default: throw new Exception("There is unsupport Data type.");
                    }
                }
            }
            catch { throw new ArgumentException("Invalid JSON document."); }
        }
コード例 #11
0
        static void __BsonParseBsonObject(BlockarObject blockarObject, BinaryReader reader)
        {
            try
            {
                var tokenStack      = new Queue <object> ();
                var isParsing       = true;
                var dataSize        = reader.ReadInt32();
                var currentPosition = (int)reader.BaseStream.Position;
                while (isParsing && (reader.BaseStream.Position - currentPosition) != dataSize)
                {
                    var rb = (BSONType)reader.ReadByte();
                    if (rb == BSONType.EndDoc)
                    {
                        break;
                    }

                    tokenStack.Enqueue(__BsonGetKeyFromBinary(reader));
                    switch (rb)
                    {
                    case BSONType.EndDoc: isParsing = false; break;

                    case BSONType.Double: tokenStack.Enqueue(reader.ReadDouble()); break;

                    case BSONType.String: tokenStack.Enqueue(__BsonGetStringFromBinary(reader)); break;

                    case BSONType.Document:
                    {
                        var inner = new BlockarObject();
                        __BsonParseBsonObject(inner, reader);
                        tokenStack.Enqueue(inner);
                    }
                    break;

                    case BSONType.Array:
                    {
                        var arr = new List <object> ();
                        __BsonParseBsonArray(arr, reader);
                        tokenStack.Enqueue(arr.ToArray());
                    }
                    break;

                    case BSONType.BinaryData: tokenStack.Enqueue(__BsonGetBinaryFromBinary(reader)); break;

                    case BSONType.Boolean: tokenStack.Enqueue(reader.ReadByte() != 0); break;

                    case BSONType.UTCTime: tokenStack.Enqueue(DateTime.FromFileTimeUtc(reader.ReadInt64())); break;

                    case BSONType.Null: tokenStack.Enqueue(null); break;

                    case BSONType.Regexp: tokenStack.Enqueue(new Regex(__BsonGetStringFromBinary(reader))); break;

                    case BSONType.JavascriptCode: tokenStack.Enqueue(__BsonGetStringFromBinary(reader)); break;

                    case BSONType.Integer: tokenStack.Enqueue(reader.ReadInt32()); break;

                    case BSONType.Integer64: tokenStack.Enqueue(reader.ReadInt64()); break;

                    default: throw new Exception("There is unsupport Data type.");
                    }
                }

                if (tokenStack.Count % 2 != 0)
                {
                    throw new ArgumentException("Invalid JSON document.");
                }

                while (tokenStack.Count != 0)
                {
                    string key   = tokenStack.Dequeue() as string;
                    object value = tokenStack.Dequeue();
                    blockarObject.Set(key, value);
                }
            }
            catch { throw new ArgumentException("Invalid JSON document."); }
        }