Subversion Repository Public Repository

Nextrek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//-----------------------------------------------------------------------
// <copyright file="JsonNetSErializer.cs" company="The Outercurve Foundation">
//    Copyright (c) 2011, The Outercurve Foundation. 
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//      http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// <author>Nathan Totten (ntotten.com), Jim Zimmerman (jimzimmerman.com) and Prabir Shrestha (prabir.me)</author>
// <website>https://github.com/facebook-csharp-sdk/facbook-csharp-sdk</website>
//-----------------------------------------------------------------------

namespace Facebook.Serializers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;
    using Newtonsoft.Json.Linq;

    /// <remarks>
    /// Install-Package Newtonsoft.Json
    /// It is recommended to set the the default json serializers during application startup.
    /// var jsonNetSerializer = new JsonNetSerializer();
    /// FacebookClient.SetDefaultJsonSerializers(jsonNetSerializer.SerializeObject, jsonNetSerializer.DeserializeObject);
    /// </remarks>
    public class JsonNetSerializer
    {
        private JsonSerializerSettings _serializerSettings;

        public JsonNetSerializer()
        {
            // Standard settings
            var isoDate = new IsoDateTimeConverter();
            isoDate.DateTimeFormat = "yyyy-MM-ddTHH:mm:sszzz";
            var settings = new JsonSerializerSettings();
            settings.Converters = settings.Converters ?? new List<JsonConverter>();
            settings.Converters.Add(isoDate);
            settings.MissingMemberHandling = MissingMemberHandling.Ignore;
            settings.NullValueHandling = NullValueHandling.Include;
            settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            settings.TypeNameHandling = TypeNameHandling.None;
            settings.ConstructorHandling = ConstructorHandling.Default;

            _serializerSettings = settings;
        }

        /// <summary>
        /// Serializes the object to json string.
        /// </summary>
        /// <param name="obj">
        /// The value.
        /// </param>
        /// <returns>
        /// The json string.
        /// </returns>
        public string SerializeObject(object obj)
        {
            return JsonConvert.SerializeObject(obj, Formatting.None, this._serializerSettings);
        }


        /// <summary>
        /// Deserializes the json string.
        /// </summary>
        /// <param name="json">
        /// The json string.
        /// </param>
        /// <param name="type">
        /// The type of object.
        /// </param>
        /// <returns>
        /// The object.
        /// </returns>
        /// <exception cref="System.Runtime.Serialization.SerializationException">
        /// Occurs when deserialization fails.
        /// </exception>
        public object DeserializeObject(string json, Type type)
        {
            if (string.IsNullOrEmpty(json))
            {
                return null;
            }
            else
            {
                object obj;

                try
                {
                    obj = JsonConvert.DeserializeObject(json, type, _serializerSettings);
                }
                catch (JsonSerializationException ex)
                {
                    throw new System.Runtime.Serialization.SerializationException(ex.Message, ex);
                }

                // If the object is a JToken we want to
                // convert it to dynamic, it if is any
                // other type we just return it.
                var jToken = obj as JToken;
                if (jToken != null)
                {
                    return ConvertJTokenToDictionary(jToken);
                }
                else
                {
                    return obj;
                }
            }
        }

        /// <summary>
        /// Deserializes the object.
        /// </summary>
        /// <typeparam name="T">The type of the object.</typeparam>
        /// <param name="json">The json.</param>
        /// <returns></returns>
        public T DeserializeObject<T>(string json)
        {
            return (T)DeserializeObject(json, typeof(T));
        }

        /// <summary>
        /// Deserialize the json string to object.
        /// </summary>
        /// <param name="json">
        /// The json string.
        /// </param>
        /// <returns>
        /// The object.
        /// </returns>
        public object DeserializeObject(string json)
        {
            if (string.IsNullOrEmpty(json))
            {
                return null;
            }

            object obj;

            try
            {
                obj = JsonConvert.DeserializeObject(json, this._serializerSettings);
            }
            catch (JsonSerializationException ex)
            {
                throw new System.Runtime.Serialization.SerializationException(ex.Message, ex);
            }

            // If the object is a JToken we want to
            // convert it to dynamic, it if is any
            // other type we just return it.
            var jToken = obj as JToken;
            if (jToken != null)
            {
                return ConvertJTokenToDictionary(jToken);
            }
            else
            {
                return obj;
            }
        }

        /// <summary>
        /// Converts the <see cref="JToken"/> to <see cref="object"/>
        /// </summary>
        /// <param name="token">
        /// The token.
        /// </param>
        /// <returns>
        /// Returns the object.
        /// </returns>
        private static object ConvertJTokenToDictionary(JToken token)
        {
            if (token == null)
            {
                return null;
            }

            var jValue = token as JValue;
            if (jValue != null)
            {
                return jValue.Value;
            }

            var jContainer = token as JArray;
            if (jContainer != null)
            {
                var jsonList = new JsonArray();
                foreach (JToken arrayItem in jContainer)
                {
                    jsonList.Add(ConvertJTokenToDictionary(arrayItem));
                }
                return jsonList;
            }

            var jsonObject = new JsonObject();
            var jsonDict = (IDictionary<string, object>)jsonObject;
            (from childToken in token where childToken is JProperty select childToken as JProperty).ToList().ForEach(property => jsonDict.Add(property.Name, ConvertJTokenToDictionary(property.Value)));
            return jsonObject;
        }
    }
}

Commits for Nextrek/Android/SmartCharging/SmartCharging_WP/packages/Facebook.7.0.6/serializers/JsonNetSerializer.cs

Diff revisions: vs.
Revision Author Commited Message
752 JMBauan picture JMBauan Wed 16 Sep, 2015 20:06:56 +0000

upload Avatar
Edit site