Ejemplo n.º 1
0
        /**
         * Retrieves and converts the deserialized value into its serialized json representation.
         */
        void SerializeValueView(JsonKeyHandle info, ref HeapString buffer)
        {
            switch (JsonKeyType(info))
            {
            case JsonValueType.Bool:
                buffer.Append(m_BoolBuffer.ElementAt(LocationInValueBuffer(info)) ? (FixedString32)"true" : (FixedString32)"false");
                break;

            case JsonValueType.Int:
                buffer.Append(m_IntBuffer.ElementAt(LocationInValueBuffer(info)));
                break;

            case JsonValueType.Float:
                buffer.Append(m_FloatBuffer.ElementAt(LocationInValueBuffer(info)));
                break;

            case JsonValueType.String:
                buffer.Append((FixedString32)"\"");
                buffer.Append(m_StringBuffer.ElementAt(LocationInValueBuffer(info)));
                buffer.Append((FixedString32)"\"");
                break;

            case JsonValueType.Array:
                SerializeArray(m_ArrayBuffer.ElementAt(LocationInValueBuffer(info)), ref buffer);
                break;

            case JsonValueType.Object:
                SerializeObject(ref m_ObjectBuffer.ElementAt(LocationInValueBuffer(info)), ref buffer);
                break;

            default:
                InvalidJsonTypeException();
                break;
            }
        }
Ejemplo n.º 2
0
        internal HeapString ToJson()
        {
            var buffer = new HeapString(Allocator.Persistent);

            SerializeObject(ref m_RootObjMap, ref buffer);
            return(buffer);
        }
        //统一不在方法内读TAG,即已经在判断时读过了
        public static HeapString Deserialize(this HeapString input, BinaryReader br)
        {
            (input as HeapRawData).Deserialize(br);
            input.StringID   = (int)BitConverter.ToInt32(input.RawData, 0).FromBigEndian();
            input.StringData = Encoding.UTF8.GetString(input.RawData, 4, input.RawData.Length - 4);

            return(input);
        }
 public unsafe TinyJsonStreamingWriter(Allocator allocator, int expectedSize = 50, int expectedDepth = 5)
 {
     m_WroteToString = false;
     m_Stack         = new SimpleStack(expectedDepth, allocator);
     m_Buffer        = new HeapString(expectedSize, allocator);
     m_Buffer.Append(((FixedString128)"{ ").GetUnsafePtr(), ((FixedString128)"{ ").Length);
     m_Stack.Push(k_EmptyObject);
 }
Ejemplo n.º 5
0
        public void HeapStringContains()
        {
            HeapString a = new HeapString("bookkeeper", Allocator.Temp);
            HeapString b = new HeapString("ookkee", Allocator.Temp);

            Assert.AreEqual(true, a.Contains(b));
            a.Dispose();
            b.Dispose();
        }
Ejemplo n.º 6
0
        public void HeapStringToStringWorks(String a)
        {
            HeapString aa = new HeapString(4, Allocator.Temp);

            aa.Append(new FixedString128(a));
            Assert.AreEqual(a, aa.ToString());
            aa.AssertNullTerminated();
            aa.Dispose();
        }
Ejemplo n.º 7
0
        public void HeapStringComparisons()
        {
            HeapString a = new HeapString("apple", Allocator.Temp);
            HeapString b = new HeapString("banana", Allocator.Temp);

            Assert.AreEqual(false, a.Equals(b));
            Assert.AreEqual(true, !b.Equals(a));
            a.Dispose();
            b.Dispose();
        }
Ejemplo n.º 8
0
        public void HeapStringLastIndexOf()
        {
            HeapString a = new HeapString("bookkeeper bookkeeper", Allocator.Temp);
            HeapString b = new HeapString("ookkee", Allocator.Temp);

            Assert.AreEqual(12, a.LastIndexOf(b));
            Assert.AreEqual(-1, b.LastIndexOf(a));
            a.Dispose();
            b.Dispose();
        }
Ejemplo n.º 9
0
        public void HeapStringAppendString()
        {
            HeapString aa = new HeapString(4, Allocator.Temp);

            aa.Append("aa");
            Assert.AreEqual("aa", aa.ToString());
            aa.Append("bb");
            Assert.AreEqual("aabb", aa.ToString());
            aa.Dispose();
        }
Ejemplo n.º 10
0
        public void HeapStringEqualsWorks(String a, String b)
        {
            HeapString aa = new HeapString(new FixedString128(a), Allocator.Temp);
            HeapString bb = new HeapString(new FixedString128(b), Allocator.Temp);

            Assert.AreEqual(aa.Equals(bb), a.Equals(b));
            aa.AssertNullTerminated();
            bb.AssertNullTerminated();
            aa.Dispose();
            bb.Dispose();
        }
Ejemplo n.º 11
0
        public void HeapStringFormatExtension1Params()
        {
            HeapString aa = new HeapString(4, Allocator.Temp);

            aa.Junk();
            FixedString32 format = "{0}";
            FixedString32 arg0   = "a";

            aa.AppendFormat(format, arg0);
            Assert.AreEqual("a", aa);
            aa.AssertNullTerminated();
            aa.Dispose();
        }
Ejemplo n.º 12
0
        public void HeapStringAppendGrows()
        {
            HeapString aa           = new HeapString(1, Allocator.Temp);
            var        origCapacity = aa.Capacity;

            for (int i = 0; i < origCapacity; ++i)
            {
                aa.Append('a');
            }
            Assert.AreEqual(origCapacity, aa.Capacity);
            aa.Append('b');
            Assert.GreaterOrEqual(aa.Capacity, origCapacity);
            Assert.AreEqual(new String('a', origCapacity) + "b", aa.ToString());
            aa.Dispose();
        }
Ejemplo n.º 13
0
        public void HeapStringFormatExtension4Params()
        {
            HeapString aa = new HeapString(Allocator.Temp);

            aa.Junk();
            FixedString32 format = "{0} {1} {2} {3}";
            FixedString32 arg0   = "a";
            FixedString32 arg1   = "b";
            FixedString32 arg2   = "c";
            FixedString32 arg3   = "d";

            aa.AppendFormat(format, arg0, arg1, arg2, arg3);
            Assert.AreEqual("a b c d", aa);
            aa.AssertNullTerminated();
            aa.Dispose();
        }
Ejemplo n.º 14
0
        public void HeapStringForEach()
        {
            HeapString     actual   = new HeapString("A🌕Z🌑", Allocator.Temp);
            FixedListInt32 expected = default;

            expected.Add('A');
            expected.Add(0x1F315);
            expected.Add('Z');
            expected.Add(0x1F311);
            int index = 0;

            foreach (var rune in actual)
            {
                Assert.AreEqual(expected[index], rune.value);
                ++index;
            }

            actual.Dispose();
        }
Ejemplo n.º 15
0
        public void HeapStringFormatExtension7Params()
        {
            HeapString aa = new HeapString(4, Allocator.Temp);

            aa.Junk();
            FixedString32 format = "{0} {1} {2} {3} {4} {5} {6}";
            FixedString32 arg0   = "a";
            FixedString32 arg1   = "b";
            FixedString32 arg2   = "c";
            FixedString32 arg3   = "d";
            FixedString32 arg4   = "e";
            FixedString32 arg5   = "f";
            FixedString32 arg6   = "g";

            aa.AppendFormat(format, arg0, arg1, arg2, arg3, arg4, arg5, arg6);
            Assert.AreEqual("a b c d e f g", aa);
            aa.AssertNullTerminated();
            aa.Dispose();
        }
Ejemplo n.º 16
0
        public void HeapStringCopyFromBytesWorks(String a)
        {
            HeapString aa = new HeapString(4, Allocator.Temp);

            aa.Junk();
            var utf8 = Encoding.UTF8.GetBytes(a);

            unsafe
            {
                fixed(byte *b = utf8)
                aa.Append(b, (ushort)utf8.Length);
            }

            Assert.AreEqual(a, aa.ToString());
            aa.AssertNullTerminated();

            aa.Append("tail");
            Assert.AreEqual(a + "tail", aa.ToString());
            aa.AssertNullTerminated();

            aa.Dispose();
        }
Ejemplo n.º 17
0
 /// <summary>
 /// <undoc />
 /// </summary>
 /// <param name="source"><undoc /></param>
 public Enumerator(HeapString source)
 {
     target  = source;
     offset  = 0;
     current = default;
 }
Ejemplo n.º 18
0
        /**
         * Serializes an object along with all of its fields back into JSON format.
         */
        void SerializeObject(ref UnsafeHashMap <FixedString128, JsonKeyHandle> objMap, ref HeapString buffer)
        {
            buffer.Append((FixedString32)"{");

            // print out fields in alphabetical order
            var keyList = objMap.GetKeyArray(Allocator.Temp);

            keyList.Sort();

            for (int i = 0; i < keyList.Length; i++)
            {
                buffer.AppendFormat((FixedString32)"\"{0}\": ", keyList[i]);
                SerializeValueView(objMap[keyList[i]], ref buffer);
                if (i < keyList.Length - 1)
                {
                    buffer.Append((FixedString32)", ");
                }
            }

            buffer.Append((FixedString32)"}");
        }