mirror of https://github.com/apache/jclouds.git
enhanced to make jsonball store primitives better
This commit is contained in:
parent
a7aa1c1964
commit
c40a14012a
|
@ -20,6 +20,8 @@ package org.jclouds.domain;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkNotNull;
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
|
||||||
|
import org.jclouds.util.Patterns;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* As String is final, using a different marker to imply this is a json object
|
* As String is final, using a different marker to imply this is a json object
|
||||||
|
@ -62,7 +64,14 @@ public class JsonBall implements java.io.Serializable, Comparable<String>, CharS
|
||||||
}
|
}
|
||||||
|
|
||||||
public JsonBall(String value) {
|
public JsonBall(String value) {
|
||||||
this.value = checkNotNull(value, "value");
|
this.value = quoteStringIfNotNumber(checkNotNull(value, "value"));
|
||||||
|
}
|
||||||
|
|
||||||
|
static String quoteStringIfNotNumber(String in) {
|
||||||
|
if (Patterns.JSON_STRING_PATTERN.matcher(in).find() && !Patterns.JSON_NUMBER_PATTERN.matcher(in).find()) {
|
||||||
|
return "\"" + in + "\"";
|
||||||
|
}
|
||||||
|
return in;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -37,6 +37,8 @@ public class Patterns {
|
||||||
public static final Pattern URL_ENCODED_PATTERN = Pattern.compile(".*%[a-fA-F0-9][a-fA-F0-9].*");
|
public static final Pattern URL_ENCODED_PATTERN = Pattern.compile(".*%[a-fA-F0-9][a-fA-F0-9].*");
|
||||||
public static final Pattern URI_PATTERN = Pattern.compile("([a-z0-9]+)://([^:]*):(.*)@(.*)");
|
public static final Pattern URI_PATTERN = Pattern.compile("([a-z0-9]+)://([^:]*):(.*)@(.*)");
|
||||||
public static final Pattern PATTERN_THAT_BREAKS_URI = Pattern.compile("[a-z0-9]+://.*/.*@.*");
|
public static final Pattern PATTERN_THAT_BREAKS_URI = Pattern.compile("[a-z0-9]+://.*/.*@.*");
|
||||||
|
public static final Pattern JSON_STRING_PATTERN = Pattern.compile("^[^\"\\{\\[].*[^\\{\\[\"]$");
|
||||||
|
public static final Pattern JSON_NUMBER_PATTERN = Pattern.compile("^[0-9]*\\.?[0-9]*$");
|
||||||
public static final Pattern PLUS_PATTERN = Pattern.compile("\\+");
|
public static final Pattern PLUS_PATTERN = Pattern.compile("\\+");
|
||||||
public static final Pattern STAR_PATTERN = Pattern.compile("\\*");
|
public static final Pattern STAR_PATTERN = Pattern.compile("\\*");
|
||||||
public static final Pattern _7E_PATTERN = Pattern.compile("%7E");
|
public static final Pattern _7E_PATTERN = Pattern.compile("%7E");
|
||||||
|
|
|
@ -54,7 +54,7 @@ public class JsonBallTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void test() {
|
public void testHash() {
|
||||||
String json = "{\"tomcat6\":{\"ssl_port\":8433}}";
|
String json = "{\"tomcat6\":{\"ssl_port\":8433}}";
|
||||||
|
|
||||||
Map<String, JsonBall> map = ImmutableMap.<String, JsonBall> of("tomcat6", new JsonBall("{\"ssl_port\":8433}"));
|
Map<String, JsonBall> map = ImmutableMap.<String, JsonBall> of("tomcat6", new JsonBall("{\"ssl_port\":8433}"));
|
||||||
|
@ -65,4 +65,38 @@ public class JsonBallTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void testList() {
|
||||||
|
String json = "{\"list\":[8431,8433]}";
|
||||||
|
|
||||||
|
Map<String, JsonBall> map = ImmutableMap.<String, JsonBall> of("list", new JsonBall("[8431,8433]"));
|
||||||
|
|
||||||
|
assertEquals(handler.apply(new HttpResponse(200, "ok", Payloads
|
||||||
|
.newStringPayload(json))), map);
|
||||||
|
assertEquals(mapper.toJson(map), json);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testString() {
|
||||||
|
String json = "{\"name\":\"fooy\"}";
|
||||||
|
|
||||||
|
Map<String, JsonBall> map = ImmutableMap.<String, JsonBall> of("name", new JsonBall("fooy"));
|
||||||
|
|
||||||
|
assertEquals(handler.apply(new HttpResponse(200, "ok", Payloads
|
||||||
|
.newStringPayload(json))), map);
|
||||||
|
assertEquals(mapper.toJson(map), json);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void testNumber() {
|
||||||
|
String json = "{\"number\":1}";
|
||||||
|
|
||||||
|
Map<String, JsonBall> map = ImmutableMap.<String, JsonBall> of("number", new JsonBall("1"));
|
||||||
|
|
||||||
|
assertEquals(handler.apply(new HttpResponse(200, "ok", Payloads
|
||||||
|
.newStringPayload(json))), map);
|
||||||
|
assertEquals(mapper.toJson(map), json);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,19 +30,64 @@ import org.testng.annotations.Test;
|
||||||
@Test(groups = "unit", testName = "jclouds.PatternsTest")
|
@Test(groups = "unit", testName = "jclouds.PatternsTest")
|
||||||
public class PatternsTest {
|
public class PatternsTest {
|
||||||
|
|
||||||
|
public void testJSON_STRING_PATTERN1() {
|
||||||
|
Matcher matcher = Patterns.JSON_STRING_PATTERN.matcher("hello");
|
||||||
|
assert (matcher.find());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testJSON_STRING_PATTERN2() {
|
||||||
|
Matcher matcher = Patterns.JSON_STRING_PATTERN.matcher("hello world!");
|
||||||
|
assert (matcher.find());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testJSON_STRING_PATTERN3() {
|
||||||
|
Matcher matcher = Patterns.JSON_STRING_PATTERN.matcher("\"hello world!\"");
|
||||||
|
assert (!matcher.find());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testJSON_STRING_PATTERN4() {
|
||||||
|
Matcher matcher = Patterns.JSON_STRING_PATTERN.matcher("[hello world!]");
|
||||||
|
assert (!matcher.find());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testJSON_STRING_PATTERN5() {
|
||||||
|
Matcher matcher = Patterns.JSON_STRING_PATTERN.matcher("{hello world!}");
|
||||||
|
assert (!matcher.find());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testJSON_NUMBER_PATTERN1() {
|
||||||
|
Matcher matcher = Patterns.JSON_NUMBER_PATTERN.matcher("1");
|
||||||
|
assert (matcher.find());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testJSON_NUMBER_PATTERN2() {
|
||||||
|
Matcher matcher = Patterns.JSON_NUMBER_PATTERN.matcher("1.1");
|
||||||
|
assert (matcher.find());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testJSON_NUMBER_PATTERN3() {
|
||||||
|
Matcher matcher = Patterns.JSON_NUMBER_PATTERN.matcher("\"1.1\"");
|
||||||
|
assert (!matcher.find());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testJSON_NUMBER_PATTERN4() {
|
||||||
|
Matcher matcher = Patterns.JSON_NUMBER_PATTERN.matcher("\"1\"");
|
||||||
|
assert (!matcher.find());
|
||||||
|
}
|
||||||
|
|
||||||
public void testREST_CONTEXT_BUILDER() {
|
public void testREST_CONTEXT_BUILDER() {
|
||||||
Matcher matcher = Patterns.REST_CONTEXT_BUILDER
|
Matcher matcher = Patterns.REST_CONTEXT_BUILDER
|
||||||
.matcher("org.jclouds.rest.RestContextBuilder<java.lang.String,java.lang.Integer>");
|
.matcher("org.jclouds.rest.RestContextBuilder<java.lang.String,java.lang.Integer>");
|
||||||
assert (matcher.find());
|
assert (matcher.find());
|
||||||
assertEquals(matcher.group(1), "org.jclouds.rest.RestContextBuilder");
|
assertEquals(matcher.group(1), "org.jclouds.rest.RestContextBuilder");
|
||||||
assertEquals(matcher.group(2), "java.lang.String");
|
assertEquals(matcher.group(2), "java.lang.String");
|
||||||
assertEquals(matcher.group(3), "java.lang.Integer");
|
assertEquals(matcher.group(3), "java.lang.Integer");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testREST_CONTEXT_BUILDERwithSpace() {
|
public void testREST_CONTEXT_BUILDERwithSpace() {
|
||||||
Matcher matcher = Patterns.REST_CONTEXT_BUILDER
|
Matcher matcher = Patterns.REST_CONTEXT_BUILDER
|
||||||
.matcher("org.jclouds.rest.RestContextBuilder<java.lang.String, java.lang.Integer>");
|
.matcher("org.jclouds.rest.RestContextBuilder<java.lang.String, java.lang.Integer>");
|
||||||
assert (matcher.find());
|
assert (matcher.find());
|
||||||
assertEquals(matcher.group(1), "org.jclouds.rest.RestContextBuilder");
|
assertEquals(matcher.group(1), "org.jclouds.rest.RestContextBuilder");
|
||||||
assertEquals(matcher.group(2), "java.lang.String");
|
assertEquals(matcher.group(2), "java.lang.String");
|
||||||
|
|
Loading…
Reference in New Issue