示例#1
0
        // sortAndTransformObject sorts object keys to make the output predictable so
        // the package can be tested; logic is here to prevent code duplication
        //~ func sortAndTransformObject(o *bytes.Buffer, cont *namedContainer) error {
        private static void SortAndTransformObject(StringBuilder o, NamedContainer cont)
        {
            //~ objectChildren, err := cont.ChildrenMap()
            //~ if err != nil {
            //~     return err
            //~ }
            var objectChildren = cont.ChildrenMap();

            //~ sortedNames := make([]string, 0, len(objectChildren))
            //~ for name, _ := range objectChildren {
            //~     sortedNames = append(sortedNames, name)
            //~ }
            //~ sort.Strings(sortedNames)
            //~ for _, name := range sortedNames {
            //~     if err := transformContainer(o, &namedContainer{name: name, Container: objectChildren[name]}); err != nil {
            //~         return err
            //~     }
            //~ }
            var sortedNames = new List <string>();

            foreach (var name in objectChildren.Keys)
            {
                sortedNames.Add(name);
            }

            sortedNames.Sort();
            foreach (var name in sortedNames)
            {
                TransformContainer(o, new NamedContainer(name, objectChildren[name]));
            }

            //~ return nil
        }
示例#2
0
        //~ func TestEncodeJSONBytes(t *testing.T) {
        public void TestEncodeJSONBytes()
        {
            //~ jsonxBytes, err := EncodeJSONBytes([]byte(testJSONOfficial))
            //~ if err != nil {
            //~     t.Fatal(err)
            //~ }
            //~ if string(jsonxBytes) != resultJSONOfficial {
            //~     t.Fatalf("official result mismatch:\ngot:\n%s\nexpected:\n%s\n", string(jsonxBytes), resultJSONOfficial)
            //~ }
            var jsonxBytes = NamedContainer.EncodeJSONBytes(testJSONOfficial.ToUtf8Bytes());

            Assert.AreEqual(resultJSONOfficial, jsonxBytes.ToUtf8String());

            //~ jsonxBytes, err = EncodeJSONBytes([]byte(testJSONHard))
            //~ if err != nil {
            //~     t.Fatal(err)
            //~ }
            //~ if string(jsonxBytes) != resultJSONHard {
            //~     t.Fatalf("hard result mismatch:\ngot:\n%s\nexpected:\n%s\n", string(jsonxBytes), resultJSONHard)
            //~ }
            jsonxBytes = NamedContainer.EncodeJSONBytes(testJSONHard.ToUtf8Bytes());
            Assert.AreEqual(resultJSONHard, jsonxBytes.ToUtf8String());
        }
示例#3
0
        //~ func transformContainer(o *bytes.Buffer, cont *namedContainer) error {
        private static void TransformContainer(StringBuilder o, NamedContainer cont)
        {
            //~var printName string
            string printName = string.Empty;

            //~ if cont.name != "" {
            //~     escapedNameBuf := bytes.NewBuffer(nil)
            //~     err := xml.EscapeText(escapedNameBuf, []byte(cont.name))
            //~     if err != nil {
            //~         return err
            //~     }
            //~     printName = fmt.Sprintf(" name=\"%s\"", escapedNameBuf.String())
            //~ }
            if (!string.IsNullOrEmpty(cont.Name))
            {
                var escapedNameBuf = XmlEscapeText(cont.Name);
                printName = $" name=\"{escapedNameBuf}\"";
            }

            //~ data := cont.Data()
            //~ switch data.(type) {
            var data = cont.Object;

            switch (data)
            {
            //~ case nil:
            //~     o.WriteString(fmt.Sprintf("<json:null%s />", printName))
            case null:
                o.Append($"<json:null{printName} />");
                break;

            //~ case bool:
            //~     o.WriteString(fmt.Sprintf("<json:boolean%s>%t</json:boolean>", printName, data))
            case bool x:
                o.Append($"<json:boolean{printName}>{data}</json:boolean>");
                break;

            //~ case json.Number:
            //~     o.WriteString(fmt.Sprintf("<json:number%s>%v</json:number>", printName, data))
            case object x when x is Int16 || x is Int32 || x is Int64 ||
                x is UInt16 || x is UInt32 || x is UInt64 ||
                x is System.Single || x is System.Double || x is System.Decimal:
                o.Append($"<json:number{printName}>{data}</json:number>");
                break;

            //~ case string:
            //~     o.WriteString(fmt.Sprintf("<json:string%s>%v</json:string>", printName, data))
            case string x:
                o.Append($"<json:string{printName}>{data}</json:string>");
                break;

            //~ case []interface{}:
            //~     o.WriteString(fmt.Sprintf("<json:array%s>", printName))
            //~     arrayChildren, err := cont.Children()
            //~     if err != nil {
            //~         return err
            //~     }
            //~     for _, child := range arrayChildren {
            //~         if err := transformContainer(o, &namedContainer{Container: child}); err != nil {
            //~             return err
            //~         }
            //~     }
            //~     o.WriteString("</json:array>")
            case IList <object> x:
                o.Append($"<json:array{printName}>");
                foreach (var child in cont.Children())
                {
                    TransformContainer(o, new NamedContainer(child));
                }
                o.Append("</json:array>");
                break;

            //~ case map[string]interface{}:
            //~     o.WriteString(fmt.Sprintf("<json:object%s>", printName))
            //~
            //~     if err := sortAndTransformObject(o, cont); err != nil {
            //~         return err
            //~     }
            //~
            //~     o.WriteString("</json:object>")
            case IDictionary <string, object> x:
                o.Append($"<json:object{printName}>");
                SortAndTransformObject(o, cont);
                o.Append("</json:object>");
                break;

            default:
                throw new NotSupportedException($"unexpected data type [{data.GetType().FullName}]");
            }

            //~ return nil
        }