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