My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
#region License, Terms and Conditions
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain [DataMember(EmitDefaultValue = false)] 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.
*/
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Web.Script.Serialization;
using pesta.Data;
using Pesta.Engine.social.spi;

namespace Pesta.Engine.protocol.conversion
{
public class DataContractJSConverter : JavaScriptConverter
{
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
return ConvertToObject(dictionary, type);
}

public object ConvertToObject(IDictionary<string, object> dictionary, Type type)
{
var obj = Activator.CreateInstance(type);
foreach (var entry in dictionary)
{
var fieldType = type.GetProperty(entry.Key).PropertyType;
var valueType = entry.Value.GetType();
if (typeof(IDictionary).IsAssignableFrom(valueType))
{
ConvertToObject((IDictionary<string, object>)entry.Value, fieldType);
}
else
{
if (typeof(IList).IsAssignableFrom(fieldType))
{
Type argType = fieldType.GetGenericArguments()[0];
var listObj = Activator.CreateInstance(fieldType);
foreach (var val in (IList)entry.Value)
{
((IList)listObj).Add(ConvertToObject((IDictionary<string, object>)val, argType));
}
}
else
{
type.GetProperty(entry.Key).SetValue(obj, entry.Value, null);
}
}
}
return obj;
}

public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
var type = obj.GetType();
var properties = type.GetProperties();
var result = new Dictionary<string, object>();
foreach (var info in properties)
{
var value = info.GetValue(obj, null);
if (value == null)
{
continue;
}
var attribs = (DataMemberAttribute[])info.GetCustomAttributes(typeof(DataMemberAttribute), false);
if (attribs.Length == 0)
{
continue;
}
string elementName = attribs[0].Name ?? info.Name;

var valueType = value.GetType();
// following needed, otherwise enums are serialised into numbers
if (valueType.IsEnum)
{
value = value.ToString();
}
// handle scenario where only one entry
else if (typeof(IRestfulCollection).IsAssignableFrom(type) &&
typeof(IList).IsAssignableFrom(valueType))
{
var entry = ((IList) value);

if (entry.Count == 1)
{
result.Add(elementName, entry[0]);
continue;
}
}
result.Add(elementName, value);
}
return result;
}

public override IEnumerable<Type> SupportedTypes
{
get
{
return new Type[]
{
typeof(Person),
typeof(Account),
typeof(Activity),
typeof(Address),
typeof(BodyType),
typeof(ListField),
typeof(MediaItem),
typeof(Message),
typeof(MessageCollection),
typeof(Name),
typeof(Organization),
typeof(Url),
typeof(RestfulCollection<Person>),
typeof(RestfulCollection<Activity>),
};
}
}
}
}

Change log

r153 by seanlinmt on Sep 3, 2009   Diff
- increase length of author field in
applications table to 1024 characters
- field type of user_id in openid_user
changed to string
- moved Jayrock.JSON to a separate project
- removed ASPNETMVC folder
- update javascript feature Libraries
- remove unused Libraries/HtmlAgilityPack
- added support for Windows Azure Table
Storage
Go to: 
Project members, sign in to write a code review

Older revisions

r150 by seanlinmt on Aug 3, 2009   Diff
- updated database
- updated json deserialization
- added RestfulEntry to handle single
entry responses
- restored pestaClient (removed in
...
r140 by seanlinmt on Jul 19, 2009   Diff
- fix some deserialization issues
- some tidy up
- code check in for raya 0.2
r136 by seanlinmt on Jul 4, 2009   Diff
- update serialization method, ATOM
deserialization not completed
- simplified OpenSocial API data
models
All revisions of this file

File info

Size: 5378 bytes, 134 lines
Powered by Google Project Hosting