Revision 2397

View differences:

org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/json/JsonManagerImpl.java
1 1
package org.gvsig.json;
2 2

  
3
import com.jayway.jsonpath.JsonPath;
3 4
import java.io.ByteArrayInputStream;
4 5
import java.io.StringWriter;
5 6
import java.math.BigDecimal;
......
384 385
        }
385 386
        return builder;
386 387
    }
388
    
389
    @Override
390
    public JsonPathContext createJSonPathContext(JsonStructure jsonObject) {
391
        return new JsonPathContextImpl(this, jsonObject);
392
    }
393

  
394
    @Override
395
    public JsonPathContext createJSonPathContext(String jsonObject) {
396
        return new JsonPathContextImpl(this, jsonObject);
397
    }
387 398
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/json/JsonPathContextImpl.java
1
/*
2
 * gvSIG. Desktop Geographic Information System.
3
 * 
4
 * Copyright (C) 2007-2020 gvSIG Association.
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License 
17
 * along with this program. If not, see <https://www.gnu.org/licenses/>. 
18
 * 
19
 * For any additional information, do not hesitate to contact us
20
 * at info AT gvsig.com, or visit our website www.gvsig.com.
21
 */
22

  
23
package org.gvsig.json;
24

  
25
import com.jayway.jsonpath.JsonPath;
26
import com.jayway.jsonpath.ReadContext;
27
import java.sql.Date;
28
import java.sql.Time;
29
import java.sql.Timestamp;
30
import javax.json.JsonArray;
31
import javax.json.JsonObject;
32
import javax.json.JsonStructure;
33
import org.gvsig.tools.dataTypes.DataTypeUtils;
34

  
35
/**
36
 *
37
 * @author gvSIG Team
38
 */
39
public class JsonPathContextImpl implements JsonPathContext {
40

  
41
    // 
42
    // https://github.com/json-path/JsonPath
43
    //
44
    
45
    private JsonStructure jsonObject;
46
    private String json;
47
    private ReadContext context;
48
    private final JsonManager manager;
49
    
50
    public JsonPathContextImpl(JsonManager manager) {
51
        this.manager = manager;
52
        this.jsonObject = null;
53
        this.context = null;
54
    }
55
    
56
    public JsonPathContextImpl(JsonManager manager, JsonStructure jsonObject) {
57
        this(manager);
58
        this.jsonObject = jsonObject;
59
        this.json = jsonObject.toString();
60
        if( this.jsonObject!=null ) {
61
            this.context = JsonPath.parse(json);
62
        }
63
    }
64
    
65
    public JsonPathContextImpl(JsonManager manager, String json) {
66
        this(manager);
67
        this.jsonObject = null;
68
        this.json = json;
69
        if( this.json!=null ) {
70
            this.context = JsonPath.parse(json);
71
        }
72
    }
73
    
74
    @Override
75
    public JsonStructure getJsonObject() {
76
        if( this.jsonObject ==null ) {
77
            if( json != null ) {
78
                this.jsonObject = this.manager.createObject(json);
79
            }
80
        }
81
        return this.jsonObject;
82
    }
83

  
84
    @Override
85
    public void setJsonObject(JsonStructure jsonObject) {
86
        this.jsonObject = jsonObject;
87
        if( this.jsonObject!=null ) {
88
            this.context = JsonPath.parse(jsonObject.toString());
89
        }
90
    }
91

  
92
    @Override
93
    public Object get(String path) {
94
        Object r = this.context.read(path);
95
        return r;
96
    }
97

  
98
    @Override
99
    public int getInt(String path, int defaultValue) {
100
        Object r = this.context.read(path);
101
        if( r == null ) {
102
            return defaultValue;
103
        }
104
        if( r instanceof Number ) {
105
            return ((Number)r).intValue();
106
        }
107
        return DataTypeUtils.toInteger(r, defaultValue);
108
    }
109

  
110
    @Override
111
    public long getLong(String path, long defaultValue) {
112
        Object r = this.context.read(path);
113
        if( r == null ) {
114
            return defaultValue;
115
        }
116
        if( r instanceof Number ) {
117
            return ((Number)r).longValue();
118
        }
119
        return DataTypeUtils.toLong(r, defaultValue);
120
    }
121

  
122
    @Override
123
    public double getDouble(String path, double defaultValue) {
124
        Object r = this.context.read(path);
125
        if( r == null ) {
126
            return defaultValue;
127
        }
128
        if( r instanceof Number ) {
129
            return ((Number)r).doubleValue();
130
        }
131
        return DataTypeUtils.toDouble(r, defaultValue);
132
    }
133

  
134
    @Override
135
    public float getFloat(String path, float defaultValue) {
136
        Object r = this.context.read(path);
137
        if( r == null ) {
138
            return defaultValue;
139
        }
140
        if( r instanceof Number ) {
141
            return ((Number)r).floatValue();
142
        }
143
        return DataTypeUtils.toFloat(r, defaultValue);
144
    }
145

  
146
    @Override
147
    public Date getDate(String path) {
148
        Object r = this.context.read(path);
149
        if( r == null ) {
150
            return null;
151
        }
152
        if( r instanceof Date ) {
153
            return (Date) r;
154
        }
155
        return (Date) DataTypeUtils.toDate(r);
156
    }
157

  
158
    @Override
159
    public Time getTime(String path) {
160
        Object r = this.context.read(path);
161
        if( r == null ) {
162
            return null;
163
        }
164
        if( r instanceof Time ) {
165
            return (Time) r;
166
        }
167
        return (Time) DataTypeUtils.toTime(r);
168
    }
169

  
170
    @Override
171
    public Timestamp getTimestamp(String path) {
172
        Object r = this.context.read(path);
173
        if( r == null ) {
174
            return null;
175
        }
176
        if( r instanceof Timestamp ) {
177
            return (Timestamp) r;
178
        }
179
        return (Timestamp) DataTypeUtils.toTimestamp(r);
180
    }
181

  
182
    @Override
183
    public JsonObject getJsonObject(String path) {
184
        Object r = this.context.read(path);
185
        if( r == null ) {
186
            return null;
187
        }
188
        return this.manager.createObject(r.toString());
189
    }
190

  
191
    @Override
192
    public JsonArray getJsonArray(String path) {
193
        Object r = this.context.read(path);
194
        if( r == null ) {
195
            return null;
196
        }
197
        return this.manager.createArray(r.toString());
198
    }
199

  
200
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.util/org.gvsig.tools.util.impl/pom.xml
42 42
      <artifactId>jgoodies-forms</artifactId>
43 43
        <scope>compile</scope>
44 44
    </dependency>
45
<!--    <dependency>
45
    <dependency>
46
        <groupId>com.jayway.jsonpath</groupId>
47
        <artifactId>json-path</artifactId>
48
        <version>2.4.0</version>
49
        <scope>compile</scope>
50
    </dependency>
51
    <!--    <dependency>
46 52
      <groupId>net.sf.cssbox</groupId>
47 53
      <artifactId>swingbox</artifactId>
48 54
      <version>1.1</version>
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.util/org.gvsig.tools.util.api/src/main/java/org/gvsig/json/Json.java
42 42
        return javax.json.Json.createReader(in);
43 43
    }
44 44

  
45
    public static JsonPathContext createJsonPathContext(JsonStructure jsonObject) {
46
        JsonManager manager = ToolsUtilLocator.getJsonManager();
47
        JsonPathContext context = manager.createJSonPathContext(jsonObject);
48
        return context;
49
    }
50

  
51
    public static JsonPathContext createJsonPathContext(String jsonObject) {
52
        JsonManager manager = ToolsUtilLocator.getJsonManager();
53
        JsonPathContext context = manager.createJSonPathContext(jsonObject);
54
        return context;
55
    }
56

  
45 57
    public static JsonReader createReader(Reader reader) {
46 58
        return javax.json.Json.createReader(reader);
47 59
    }
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.util/org.gvsig.tools.util.api/src/main/java/org/gvsig/json/JsonPathContext.java
1
/*
2
 * gvSIG. Desktop Geographic Information System.
3
 * 
4
 * Copyright (C) 2007-2020 gvSIG Association.
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License 
17
 * along with this program. If not, see <https://www.gnu.org/licenses/>. 
18
 * 
19
 * For any additional information, do not hesitate to contact us
20
 * at info AT gvsig.com, or visit our website www.gvsig.com.
21
 */
22
package org.gvsig.json;
23

  
24
import java.sql.Date;
25
import java.sql.Time;
26
import java.sql.Timestamp;
27
import javax.json.JsonArray;
28
import javax.json.JsonObject;
29
import javax.json.JsonStructure;
30

  
31
/**
32
 *
33
 * @author gvSIG Team
34
 */
35
public interface JsonPathContext {
36
    public JsonStructure getJsonObject();
37
    public void setJsonObject(JsonStructure json);
38
    
39
    public Object get(String path);
40
    
41
    public int getInt(String path, int defaultValue);
42
    public long getLong(String path, long defaultValue);
43
    public double getDouble(String path, double defaultValue);
44
    public float getFloat(String path, float defaultValue);
45
    public Date getDate(String path);
46
    public Time getTime(String path);
47
    public Timestamp getTimestamp(String path);
48
    public JsonObject getJsonObject(String path);
49
    public JsonArray getJsonArray(String path);
50
    
51
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.util/org.gvsig.tools.util.api/src/main/java/org/gvsig/json/JsonManager.java
69 69
    public DynObject addAll(DynObject target, JsonObject json);
70 70
    
71 71
    public Map toHashMap(JsonObject json);
72

  
73
    public JsonPathContext createJSonPathContext(JsonStructure jsonObject);
72 74
    
75
    public JsonPathContext createJSonPathContext(String json);
73 76
}

Also available in: Unified diff