1
2
3
4
5
6 using System;
7 using System.Collections;
8
9 namespace System.Json
10 {
11 public delegate void* JsonClassCreatorDelegate(JsonValue* value);
12
13 public class JsonClassFactory
14 {
15 static JsonClassFactory() : instance(new JsonClassFactory())
16 {
17 }
18 public static JsonClassFactory& Instance()
19 {
20 return *instance;
21 }
22 public void RegisterJsonClass(const ustring& jsonClassName, JsonClassCreatorDelegate creator)
23 {
24 creatorMap[jsonClassName] = creator;
25 }
26 public void* CreateJsonClass(const ustring& jsonClassName, JsonValue* value)
27 {
28 HashMap<ustring, JsonClassCreatorDelegate>.ConstIterator it = creatorMap.CFind(jsonClassName);
29 if (it != creatorMap.CEnd())
30 {
31 JsonClassCreatorDelegate creator = it->second;
32 return creator(value);
33 }
34 else
35 {
36 throw JsonImportException("no JSON creator delegate registered for JSON class '" + ToUtf8(jsonClassName) + "'");
37 }
38 }
39 private static UniquePtr<JsonClassFactory> instance;
40 private HashMap<ustring, JsonClassCreatorDelegate> creatorMap;
41 }
42
43 public void RegisterJsonClass(const string& jsonClassName, JsonClassCreatorDelegate creator)
44 {
45 RegisterJsonClass(ToUtf32(jsonClassName), creator);
46 }
47
48 public void RegisterJsonClass(const wstring& jsonClassName, JsonClassCreatorDelegate creator)
49 {
50 RegisterJsonClass(ToUtf32(jsonClassName), creator);
51 }
52
53 public void RegisterJsonClass(const ustring& jsonClassName, JsonClassCreatorDelegate creator)
54 {
55 JsonClassFactory.Instance().RegisterJsonClass(jsonClassName, creator);
56 }
57 }