1 // Licensed to the Apache Software Foundation(ASF) under one
2 // or more contributor license agreements.See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership.The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 using System;
19 using System.Collections;
20 using System.Collections.Generic;
21 using System.Globalization;
22 using System.Text;
23 using Thrift.Protocol;
24 
25 namespace Thrift.Protocol
26 {
27 
28 
29     public static class ToStringExtensions
30     {
ToString(this object self, StringBuilder sb, bool first = true)31         public static void ToString(this object self, StringBuilder sb, bool first = true)
32         {
33             if (!first)
34                 sb.Append(", ");
35 
36             bool first_child = true;
37             if (self is string) // string is IEnumerable
38             {
39                 sb.Append('"');
40                 sb.Append(self);
41                 sb.Append('"');
42             }
43             else if (self is IDictionary)
44             {
45                 sb.Append("{ ");
46                 foreach (DictionaryEntry pair in (self as IDictionary))
47                 {
48                     if (first_child)
49                         first_child = false;
50                     else
51                         sb.Append(',');
52 
53                     sb.Append("{ ");
54                     pair.Key.ToString(sb);
55                     sb.Append(", ");
56                     pair.Value.ToString(sb);
57                     sb.Append('}');
58                 }
59                 sb.Append('}');
60             }
61             else if (self is IEnumerable)
62             {
63                 sb.Append("{ ");
64                 foreach (var elm in (self as IEnumerable))
65                 {
66                     elm.ToString(sb, first_child);
67                     first_child = false;
68                 }
69                 sb.Append('}');
70             }
71             else if (self is TBase)
72             {
73                 sb.Append((self as TBase).ToString());
74             }
75             else if (self is double)
76             {
77                 sb.Append(((double)self).ToString(CultureInfo.InvariantCulture));
78             }
79             else
80             {
81                 sb.Append(self != null ? self.ToString() : "<null>");
82             }
83         }
84     }
85 
86 
87 }
88