Beispiel #1
0
        /// <summary>
        /// Writes the property name of a name/value pair on a Json object.
        /// </summary>
        /// <param name="name">The name of the property.</param>
        public override void WritePropertyName(string name)
        {
            base.WritePropertyName(name);

            int ref_index = 0;

            if (!this.PropertyReferences.TryGetValue(name, out ref_index))
            {
                bool   toShared = false;
                byte[] buf;
                if (SmileUtil.IsASCIIBytes(name, out buf))
                {
                    toShared = _writer.WriteAsciiPropertyName(buf);
                }
                else
                {
                    toShared = _writer.WriteUnicodePropertyName(buf);
                }

                if (toShared)
                {
                    ref_index = CurrentPropertyReferenceIndex;
                    this.PropertyReferences.Add(name, ref_index);
                    CurrentPropertyReferenceIndex++;
                }
            }
            else
            {
                _writer.WriteShortReferencePropertyName(ref_index);
            }
        }
        public void WriteString(string value)
        {
            byte[] buf;
            bool   isASCII = SmileUtil.IsASCIIBytes(value, out buf);

            if (buf.Length == 0)
            {
                this._Write(VALUE_CONSTANTS.EmptyString);
            }

            else if (isASCII)
            {
                if (buf.Length >= 1 && buf.Length <= 64)
                {
                    this.WriteShortString((byte)((byte)VALUE_CONSTANTS.TinyAscii_BEGIN + buf.Length - 1), buf);
                }
                else
                {
                    this.WriteFCString((byte)VALUE_CONSTANTS.LongAsciiText, buf);
                }
            }
            else
            {
                if (buf.Length < 2)
                {
                    throw new FormatException("bad utf-8 string.");
                }
                else if (buf.Length >= 2 && buf.Length <= 64)
                {
                    this.WriteShortString((byte)((byte)VALUE_CONSTANTS.TinyUnicode_BEGIN + buf.Length - 2), buf);
                }
                else
                {
                    this.WriteFCString((byte)VALUE_CONSTANTS.LongUnicodeText, buf);
                }
            }
        }