Json.cpp

Zbigniew Rebacz, 03/01/2015 12:09 AM

Download (3.55 KB)

 
1
#include "ide.h"
2

    
3
class JsonView : public TopWindow {
4
        typedef JsonView CLASSNAME;
5

    
6
public:
7
        TreeCtrl              tree;
8
        LineEdit              view;
9
        FrameTop<StaticRect>  errorbg;
10
        Label                 error;
11
        
12
public:
13
        JsonView();
14
        
15
        virtual bool Key(dword key, int count);
16
        virtual void Close();
17

    
18
        void Load(const String& json);
19
        void CopyPath();
20

    
21
        void Serialize(Stream& s);
22

    
23
private:
24
        void   Reset();
25
        String Load0(const String& json);
26
        
27
        int AddNode(int parent_id, const Value& id, const String& name, const Value& v);
28
};
29

    
30
JsonView::JsonView()
31
{
32
        Title("JSON view");
33
        Sizeable().Zoomable();
34
        Icon(IdeCommonImg::xml());
35
        
36
        Add(tree.SizePos());         
37
        Add(view.SizePos());
38
        
39
        error.SetFont(Arial(20)).SetInk(Red);
40
        errorbg.Height(25).Add(error.SizePos());
41
        view.SetReadOnly();
42
        view.SetColor(LineEdit::PAPER_READONLY, SColorPaper());
43
        tree.SetDisplay(QTFDisplay());
44
        tree.NoRoot();
45
        tree.WhenLeftDouble = THISBACK(CopyPath);
46
}
47

    
48
bool JsonView::Key(dword key, int count)
49
{
50
        if(key == K_ESCAPE) {
51
                Close();
52
                return true;
53
        }
54
        return false;
55
}
56

    
57
void JsonView::Load(const String& json)
58
{
59
        Reset();
60
        
61
        String parsingError = Load0(json);
62
        if(parsingError.GetCount() > 0) {
63
                parsingError.Set(0, ToLower(parsingError[0]));
64
                
65
                error = "Json parsing error: \"" + parsingError + "\".";
66
                AddFrame(errorbg);
67
                view.Show();
68
                view <<= json;
69
                
70
                return;
71
        }
72
        
73
        tree.Show();
74
        tree.SetFocus();
75
}
76

    
77
void JsonView::CopyPath()
78
{
79
        int id = tree.GetCursor();
80
        String path;
81
        while(id) {
82
                Value k = tree.Get(id);
83
                if(!IsNull(k)) {
84
                        if(IsNumber(k))
85
                                path = "[" + AsString(k) + "]" + path;
86
                        if(IsString(k))
87
                                path = "[" + AsCString(String(k)) + "]" + path;
88
                }
89
                id = tree.GetParent(id);
90
        }
91
        WriteClipboardText(path);
92
}
93

    
94
void JsonView::Serialize(Stream& s)
95
{
96
        int version = 0;
97
        s / version;
98
        SerializePlacement(s);
99
}
100

    
101
void JsonView::Close()
102
{
103
        StoreToGlobal(*this, "JSONview");
104
        TopWindow::Close();
105
}
106

    
107
void JsonView::Reset()
108
{
109
        RemoveFrame(errorbg);
110
        view.Clear();
111
        view.Hide();
112
        tree.Clear();
113
        tree.Hide();
114
}
115

    
116
String JsonView::Load0(const String& json)
117
{
118
        String parsingError;
119
        
120
        try {
121
                tree.Open(AddNode(0, Null, "JSON", ParseJSON(json)));
122
        }
123
        catch(const Exc& e) {
124
                parsingError = e;
125
        }
126
        
127
        return parsingError;
128
}
129

    
130
int JsonView::AddNode(int parent_id, const Value& id, const String& name, const Value& v)
131
{
132
        if(IsError(v)) {
133
                // TODO: Replace with JsonExc or something that is more accurate in this situation.
134
                String errorText = GetErrorText(v);
135
                errorText.Remove(0, errorText.Find(" ") + 1);
136
                throw Exc(errorText);
137
        }
138
        else
139
        if(v.Is<ValueMap>()) {
140
                ValueMap m = v;
141
                parent_id = tree.Add(parent_id, IdeImg::JsonStruct(), id, "[G1 [* " + name);
142
                for(int i = 0; i < m.GetCount(); i++)
143
                        AddNode(parent_id, m.GetKey(i), "[@B \1" + String(m.GetKey(i)) + "\1:]", m.GetValue(i));
144
        }
145
        else
146
        if(v.Is<ValueArray>()) {
147
                parent_id = tree.Add(parent_id, IdeImg::JsonArray(), id, "[G1 [* " + name);
148
                for(int i = 0; i < v.GetCount(); i++)
149
                        AddNode(parent_id, i, "[@c " + AsString(i) + ":]", v[i]);
150
        }
151
        else {
152
                String qtf = "[G1 [* " + name + "]";
153
                Image img = IdeImg::JsonNumber();
154
                if(IsString(v)) {
155
                        img = IdeImg::JsonString();
156
                        if(IsNull(v))
157
                                qtf << "[*@g  Null";
158
                        else
159
                                qtf << "[@r \1 " + AsCString(String(v));
160
                }
161
                else {
162
                        if(v.Is<bool>())
163
                                img = IdeImg::JsonBool();
164
                        if(IsNull(v))
165
                                qtf << "[*@g  Null";
166
                        else
167
                                qtf << "\1 " + AsString(v);
168
                }
169
                parent_id = tree.Add(parent_id, img, id, qtf);
170
        }
171
        return parent_id;
172
}
173

    
174
void Ide::Json()
175
{
176
        static JsonView dlg;
177
        dlg.Load(editor.IsSelection() ? editor.GetSelection() : editor.Get());
178
        if(!dlg.IsOpen()) {
179
                LoadFromGlobal(dlg, "JSONview");
180
                dlg.OpenMain();
181
        }
182
}