1 module uim.json;
2 
3 /++ 
4  The file type for JSON files is ".json"
5  The MIME type for JSON text is "application/json"
6  +/
7 import std.stdio;
8 import std..string;
9 
10 import vibe.d;
11 
12 public import uim.core;
13 public import uim.oop;
14 
15 public import uim.json.convert;
16 public import uim.json.classes.data;
17 
18 public import uim.json.classes.value;
19 public import uim.json.classes.number;
20 public import uim.json.classes.object_;
21 public import uim.json.classes.string_;
22 public import uim.json.classes.array_;
23 public import uim.json.classes.boolean;
24 public import uim.json.classes.null_;
25 
26 public import uim.json.schema;
27 
28 size_t positionOfString(string text, string searchString, bool strict) { return positionOfString(text, searchString, 0, strict); }
29 size_t positionOfString(string text, string searchString, size_t start = 0, bool strict = false) {
30 	if (text.empty) return -1;	
31 	auto subString = text[start..$];
32 	if (subString.empty) return -1;	
33 	if (searchString.empty) return -1;	
34 
35 	auto pos = subString.indexOf(searchString);
36 	if (pos == -1) return -1;	
37 	if (pos == 0) return start;
38 
39 	if (strict) {
40 		if (subString[pos-1] == '\\') pos = subString.positionOfString(searchString, pos+1, strict);
41 	}
42 	if (pos == -1) return -1;	
43 
44 	return pos+start;
45 }
46 size_t[] positionsOfString(string text, string searchString, bool strict = false) { return positionsOfString(text, searchString, 0, strict); }
47 size_t[] positionsOfString(string text, string searchString, size_t start = 0, bool strict = false) {
48 	size_t[] positions;
49 	size_t lastPos = -1;
50 	do {
51 		lastPos = text.positionOfString(searchString, lastPos+1, strict);
52 		if (lastPos != -1) positions ~= lastPos;
53 	} while(lastPos != -1);
54 
55 	return positions;
56 }
57 
58 size_t[][string] positionsOfStrings(string text, string[] searchStrings, bool strict = false) { return positionsOfStrings(text, searchStrings, 0, strict); }
59 size_t[][string] positionsOfStrings(string text, string[] searchStrings, size_t start = 0, bool strict = false) {
60 	size_t[][string] positions;
61 	foreach(str; searchStrings) positions[str] = text.positionsOfString(str, start, strict);
62 	return positions;
63 }
64 
65 size_t[] positionOfStrings(string text, string[] searchStrings, bool strict = false) { return positionOfStrings(text, searchStrings, 0, strict); }
66 size_t[] positionOfStrings(string text, string[] searchStrings, size_t start = 0, bool strict = false) {
67 	size_t[] positions;
68 	foreach(str; searchStrings) positions ~= text.positionOfString(str, start, strict);
69 	return positions;
70 }
71 
72 bool stringExists(string text, string searchString, bool strict = false) {
73 	auto pos = text.indexOf(searchString);
74 	if (pos == -1) return false;
75 
76 	if (pos == 0) return true;
77 	if (strict) while ((text[pos-1] == '\\') && (pos != -1)) {
78 		pos = text.indexOf(searchString, pos+1);
79 	}
80 	if (pos == -1) return false;
81 	return true;
82 }
83 string subStringBetweenStrings(string text, string leftString, string rightString, bool strict = false) {
84 	if (text.empty) return null;
85 	if (leftString.empty) return null;
86 	if (rightString.empty) return null;
87 
88 	auto leftPos = text.positionOfString(leftString, 0, strict);
89 	if (leftPos == -1) return null;
90 
91 	auto rightPos = text.positionOfString(rightString, leftPos+1, strict);
92 	if (rightPos == -1) return null;
93 
94 	return text[leftPos+1..rightPos];
95 }
96 string readNextFieldName(string jsonText, bool strict = false) {
97 	return jsonText.subStringBetweenStrings("\"", "\"", strict);
98 }
99 
100 class JSON {
101 	static DJSONValue fromText(string text) {
102 		DJSONValue result;
103 		
104 		if (auto txt = strip(text)) {
105 			switch (txt[0]) {
106 				case '{': return JSONObject(text);
107 //				case '[': return JSONArray(text);
108 				default: writeln("Wrong character: ", txt[0]);
109 			}
110 		}
111 		return null; 
112 	}
113 	
114 	this() {}
115 }