DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SerializeData.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 using System.Collections.Generic;
19 
20 namespace Deveel.Data.Serialization {
21  public sealed class SerializeData {
22  private readonly Dictionary<string, KeyValuePair<Type, object>> values;
23 
24  internal SerializeData(Type graphType) {
25  GraphType = graphType;
26  values = new Dictionary<string, KeyValuePair<Type, object>>();
27  }
28 
29  public Type GraphType { get; private set; }
30 
31  internal IEnumerable<KeyValuePair<string, KeyValuePair<Type, object>>> Values {
32  get { return values; }
33  }
34 
35  public void SetValue(string key, Type type, object value) {
36  if (String.IsNullOrEmpty(key))
37  throw new ArgumentNullException("key");
38  if (type == null)
39  throw new ArgumentNullException("type");
40 
41  if (type.IsArray) {
42  if (!IsSupported(type.GetElementType()))
43  throw new NotSupportedException(String.Format("The element type '{0}' of the array is not supported.",
44  type.GetElementType()));
45  } else if (!type.IsArray && !IsSupported(type))
46  throw new NotSupportedException(String.Format("The type '{0}' is not supported.", type));
47 
48  if (value != null && !type.IsInstanceOfType(value))
49  throw new ArgumentException(
50  String.Format("The specified object value is not assignable from the type '{0}' specified.", type));
51 
52  values[key] = new KeyValuePair<Type, object>(type, value);
53  }
54 
55  private static bool IsSupported(Type type) {
56  return type.IsPrimitive ||
57  type == typeof (string) ||
58  Attribute.IsDefined(type, typeof(SerializableAttribute));
59  }
60 
61  public void SetValue(string key, object value) {
62  if (value == null)
63  return;
64 
65  var type = value.GetType();
66  SetValue(key, type, value);
67  }
68 
69  public void SetValue(string key, bool value) {
70  SetValue(key, typeof(bool), value);
71  }
72 
73  public void SetValue(string key, byte value) {
74  SetValue(key, typeof(byte), value);
75  }
76 
77  public void SetValue(string key, short value) {
78  SetValue(key, typeof(short), value);
79  }
80 
81  public void SetValue(string key, int value) {
82  SetValue(key, typeof(int), value);
83  }
84 
85  public void SetValue(string key, long value) {
86  SetValue(key, typeof(long), value);
87  }
88 
89  public void SetValue(string key, float value) {
90  SetValue(key, typeof(float), value);
91  }
92 
93  public void SetValue(string key, double value) {
94  SetValue(key, typeof(double), value);
95  }
96 
97  public void SetValue(string key, string value) {
98  SetValue(key, typeof(string), value);
99  }
100  }
101 }
void SetValue(string key, long value)
void SetValue(string key, int value)
void SetValue(string key, Type type, object value)
void SetValue(string key, object value)
readonly Dictionary< string, KeyValuePair< Type, object > > values
void SetValue(string key, string value)
void SetValue(string key, byte value)
void SetValue(string key, float value)
void SetValue(string key, short value)
void SetValue(string key, bool value)
void SetValue(string key, double value)