Browse Source

Release preps.

pull/8/merge
Kalle Stenflo 13 years ago
parent
commit
65e52d5040
  1. 10
      README
  2. 24
      json-path/src/main/java/com/jayway/jsonpath/JsonModel.java
  3. 83
      json-path/src/main/java/com/jayway/jsonpath/internal/JsonFormatter.java
  4. 5
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayIndexFilter.java
  5. 14
      json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java
  6. 9
      json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java
  7. 28
      pom.xml

10
README

@ -1,5 +1,15 @@
Java DSL for reading and testing JSON documents. Java DSL for reading and testing JSON documents.
------------------------------------------
0.8.1 2012-04-16
------------------------------------------
- Fixed issue http://code.google.com/p/json-path/issues/detail?id=11
- HttpProvider SPI
- Improved JavaDoc
- More experimental work on JsonModel
------------------------------------------ ------------------------------------------
0.8.0 2012-03-08 0.8.0 2012-03-08
------------------------------------------ ------------------------------------------

24
json-path/src/main/java/com/jayway/jsonpath/JsonModel.java

@ -15,6 +15,7 @@
package com.jayway.jsonpath; package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.ConvertUtils; import com.jayway.jsonpath.internal.ConvertUtils;
import com.jayway.jsonpath.internal.JsonFormatter;
import com.jayway.jsonpath.internal.PathToken; import com.jayway.jsonpath.internal.PathToken;
import com.jayway.jsonpath.internal.IOUtils; import com.jayway.jsonpath.internal.IOUtils;
import com.jayway.jsonpath.spi.JsonProvider; import com.jayway.jsonpath.spi.JsonProvider;
@ -122,8 +123,12 @@ public class JsonModel {
return jsonProvider.isMap(jsonObject); return jsonProvider.isMap(jsonObject);
} }
/**
* Prints this JsonModel to standard out
*/
public void print(){ public void print(){
System.out.println(toJson()); String json = toJson();
System.out.println(JsonFormatter.prettyPrint(json));
} }
/** /**
@ -324,7 +329,22 @@ public class JsonModel {
* @return model as Json * @return model as Json
*/ */
public String toJson() { public String toJson() {
return jsonProvider.toJson(jsonObject); return toJson(false);
}
/**
* Creates a JSON representation of this JsonModel
*
* @param prettyPrint if the model should be pretty printed
* @return
*/
public String toJson(boolean prettyPrint) {
String json = jsonProvider.toJson(jsonObject);
if(prettyPrint)
return JsonFormatter.prettyPrint(json);
else
return json;
} }
/** /**

83
json-path/src/main/java/com/jayway/jsonpath/internal/JsonFormatter.java

@ -0,0 +1,83 @@
package com.jayway.jsonpath.internal;
public class JsonFormatter
{
private static final String INDENT = " ";
private static final String NEW_LINE = System.getProperty("line.separator");
private static void appendIndent(StringBuilder sb, int count)
{
for (; count > 0; --count) sb.append(INDENT);
}
private static boolean isEscaped(StringBuilder sb, int index)
{
boolean escaped = false;
while (index > 0 && sb.charAt(--index) == '\\') escaped = !escaped;
return escaped;
}
public static String prettyPrint(String input)
{
StringBuilder output = new StringBuilder(input.length() * 2);
boolean quoteOpened = false;
int depth = 0;
for(int i=0; i<input.length(); ++i)
{
char ch = input.charAt(i);
switch (ch)
{
case '{':
case '[':
output.append(ch);
if (!quoteOpened)
{
output.append(NEW_LINE);
appendIndent(output, ++depth);
}
break;
case '}':
case ']':
if (quoteOpened)
output.append(ch);
else
{
output.append(NEW_LINE);
appendIndent(output, --depth);
output.append(ch);
}
break;
case '"':
case '\'':
output.append(ch);
if (quoteOpened)
{
if (!isEscaped(output, i))
quoteOpened = false;
}
else quoteOpened = true;
break;
case ',':
output.append(ch);
if (!quoteOpened)
{
output.append(NEW_LINE);
appendIndent(output, depth);
}
break;
case ':':
if (quoteOpened) output.append(ch);
else output.append(" : ");
break;
default:
if (quoteOpened || !(ch == ' '))
output.append(ch);
break;
}
}
return output.toString();
}
}

5
json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayIndexFilter.java

@ -61,8 +61,11 @@ public class ArrayIndexFilter extends PathTokenFilter {
} else { } else {
String[] indexArr = trimmedCondition.split(","); String[] indexArr = trimmedCondition.split(",");
if (indexArr.length == 1) { if(src.isEmpty()){
return result;
}
if (indexArr.length == 1) {
return src.get(Integer.parseInt(indexArr[0])); return src.get(Integer.parseInt(indexArr[0]));
} else { } else {

14
json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java

@ -2,7 +2,11 @@ package com.jayway.jsonpath;
import org.junit.Test; import org.junit.Test;
import java.util.List;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNull; import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;
/** /**
* Created by IntelliJ IDEA. * Created by IntelliJ IDEA.
@ -26,8 +30,14 @@ public class IssuesTest {
@Test @Test
public void issue_11() throws Exception { public void issue_11() throws Exception {
//String json = "{ \"foo\" : [] }"; String json = "{ \"foo\" : [] }";
//JsonPath.read(json, "$.foo[?(@.rel= 'item')][0].uri"); List<String> result = JsonPath.read(json, "$.foo[?(@.rel= 'item')][0].uri");
System.out.println(JsonPath.compile("$.foo[?(@.rel= 'item')][0].uri").isPathDefinite());
System.out.println(JsonPath.compile("$.foo[?(@.rel= 'item')][0]").isPathDefinite());
System.out.println(JsonPath.compile("$.foo[?(@.rel= 'item')]").isPathDefinite());
assertTrue(result.isEmpty());
} }
} }

9
json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java

@ -56,7 +56,14 @@ public class JsonModelTest {
" }\n" + " }\n" +
"}"; "}";
@Test
public void a_model_can_be_pretty_printed() throws Exception {
JsonModel model = JsonModel.model(DOCUMENT);
model.print();
}
@Test @Test
public void has_path_validates() throws Exception { public void has_path_validates() throws Exception {
assertFalse(JsonModel.model(DOCUMENT).hasPath("store.invalid")); assertFalse(JsonModel.model(DOCUMENT).hasPath("store.invalid"));

28
pom.xml

@ -212,7 +212,13 @@
<dependency> <dependency>
<groupId>net.minidev</groupId> <groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId> <artifactId>json-smart</artifactId>
<version>1.1</version> <version>1.1.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency> </dependency>
<dependency> <dependency>
@ -234,26 +240,6 @@
<version>1.2.1</version> <version>1.2.1</version>
</dependency> </dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>

Loading…
Cancel
Save