Mercurial > hg > Database > jungle-sharp
comparison src/main/csharp/jp.ac.u-ryukyu.ie.cr/jungle-main/persistent/msgpack/src/BoxingPacker.cs @ 17:01a08cf4b2d9
Liq Files
author | Kazuma |
---|---|
date | Mon, 07 Nov 2016 01:05:24 +0900 |
parents | abe0c247f5a5 |
children |
comparison
equal
deleted
inserted
replaced
16:8f1ce942abfc | 17:01a08cf4b2d9 |
---|---|
1 // | |
2 // Copyright 2011 Kazuki Oikawa | |
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; | |
19 using System.Collections.Generic; | |
20 using System.IO; | |
21 using System.Reflection; | |
22 | |
23 namespace MsgPack | |
24 { | |
25 public class BoxingPacker | |
26 { | |
27 static Type KeyValuePairDefinitionType; | |
28 | |
29 static BoxingPacker () | |
30 { | |
31 KeyValuePairDefinitionType = typeof (KeyValuePair<object,object>).GetGenericTypeDefinition (); | |
32 } | |
33 | |
34 public void Pack (Stream strm, object o) | |
35 { | |
36 MsgPackWriter writer = new MsgPackWriter (strm); | |
37 Pack (writer, o); | |
38 } | |
39 | |
40 public byte[] Pack (object o) | |
41 { | |
42 using (MemoryStream ms = new MemoryStream ()) { | |
43 Pack (ms, o); | |
44 return ms.ToArray (); | |
45 } | |
46 } | |
47 | |
48 void Pack (MsgPackWriter writer, object o) | |
49 { | |
50 if (o == null) { | |
51 writer.WriteNil (); | |
52 return; | |
53 } | |
54 | |
55 Type t = o.GetType (); | |
56 if (t.IsPrimitive) { | |
57 if (t.Equals (typeof (int))) writer.Write ((int)o); | |
58 else if (t.Equals (typeof (uint))) writer.Write ((uint)o); | |
59 else if (t.Equals (typeof (float))) writer.Write ((float)o); | |
60 else if (t.Equals (typeof (double))) writer.Write ((double)o); | |
61 else if (t.Equals (typeof (long))) writer.Write ((long)o); | |
62 else if (t.Equals (typeof (ulong))) writer.Write ((ulong)o); | |
63 else if (t.Equals (typeof (bool))) writer.Write ((bool)o); | |
64 else if (t.Equals (typeof (byte))) writer.Write ((byte)o); | |
65 else if (t.Equals (typeof (sbyte))) writer.Write ((sbyte)o); | |
66 else if (t.Equals (typeof (short))) writer.Write ((short)o); | |
67 else if (t.Equals (typeof (ushort))) writer.Write ((ushort)o); | |
68 else throw new NotSupportedException (); // char? | |
69 return; | |
70 } | |
71 | |
72 IDictionary dic = o as IDictionary; | |
73 if (dic != null) { | |
74 writer.WriteMapHeader (dic.Count); | |
75 foreach (System.Collections.DictionaryEntry e in dic) { | |
76 Pack (writer, e.Key); | |
77 Pack (writer, e.Value); | |
78 } | |
79 return; | |
80 } | |
81 | |
82 if (t.IsArray) { | |
83 Array ary = (Array)o; | |
84 Type et = t.GetElementType (); | |
85 | |
86 // KeyValuePair<K,V>[] (Map Type) | |
87 if (et.IsGenericType && et.GetGenericTypeDefinition ().Equals (KeyValuePairDefinitionType)) { | |
88 PropertyInfo propKey = et.GetProperty ("Key"); | |
89 PropertyInfo propValue = et.GetProperty ("Value"); | |
90 writer.WriteMapHeader (ary.Length); | |
91 for (int i = 0; i < ary.Length; i ++) { | |
92 object e = ary.GetValue (i); | |
93 Pack (writer, propKey.GetValue (e, null)); | |
94 Pack (writer, propValue.GetValue (e, null)); | |
95 } | |
96 return; | |
97 } | |
98 | |
99 // Array | |
100 writer.WriteArrayHeader (ary.Length); | |
101 for (int i = 0; i < ary.Length; i ++) | |
102 Pack (writer, ary.GetValue (i)); | |
103 return; | |
104 } | |
105 } | |
106 | |
107 public object Unpack (Stream strm) | |
108 { | |
109 MsgPackReader reader = new MsgPackReader (strm); | |
110 return Unpack (reader); | |
111 } | |
112 | |
113 public object Unpack (byte[] buf, int offset, int size) | |
114 { | |
115 using (MemoryStream ms = new MemoryStream (buf, offset, size)) { | |
116 return Unpack (ms); | |
117 } | |
118 } | |
119 | |
120 public object Unpack (byte[] buf) | |
121 { | |
122 return Unpack (buf, 0, buf.Length); | |
123 } | |
124 | |
125 object Unpack (MsgPackReader reader) | |
126 { | |
127 if (!reader.Read ()) | |
128 throw new FormatException (); | |
129 | |
130 switch (reader.Type) { | |
131 case TypePrefixes.PositiveFixNum: | |
132 case TypePrefixes.NegativeFixNum: | |
133 case TypePrefixes.Int8: | |
134 case TypePrefixes.Int16: | |
135 case TypePrefixes.Int32: | |
136 return reader.ValueSigned; | |
137 case TypePrefixes.Int64: | |
138 return reader.ValueSigned64; | |
139 case TypePrefixes.UInt8: | |
140 case TypePrefixes.UInt16: | |
141 case TypePrefixes.UInt32: | |
142 return reader.ValueUnsigned; | |
143 case TypePrefixes.UInt64: | |
144 return reader.ValueUnsigned64; | |
145 case TypePrefixes.True: | |
146 return true; | |
147 case TypePrefixes.False: | |
148 return false; | |
149 case TypePrefixes.Float: | |
150 return reader.ValueFloat; | |
151 case TypePrefixes.Double: | |
152 return reader.ValueDouble; | |
153 case TypePrefixes.Nil: | |
154 return null; | |
155 case TypePrefixes.FixRaw: | |
156 case TypePrefixes.Raw16: | |
157 case TypePrefixes.Raw32: | |
158 byte[] raw = new byte[reader.Length]; | |
159 reader.ReadValueRaw (raw, 0, raw.Length); | |
160 return raw; | |
161 case TypePrefixes.FixArray: | |
162 case TypePrefixes.Array16: | |
163 case TypePrefixes.Array32: | |
164 object[] ary = new object[reader.Length]; | |
165 for (int i = 0; i < ary.Length; i ++) | |
166 ary[i] = Unpack (reader); | |
167 return ary; | |
168 case TypePrefixes.FixMap: | |
169 case TypePrefixes.Map16: | |
170 case TypePrefixes.Map32: | |
171 IDictionary<object, object> dic = new Dictionary<object, object> ((int)reader.Length); | |
172 int count = (int)reader.Length; | |
173 for (int i = 0; i < count; i ++) { | |
174 object k = Unpack (reader); | |
175 object v = Unpack (reader); | |
176 dic.Add (k, v); | |
177 } | |
178 return dic; | |
179 default: | |
180 throw new FormatException (); | |
181 } | |
182 } | |
183 } | |
184 } |