finalize abstracting json into xcontent

This commit is contained in:
kimchy 2010-04-29 02:11:53 +03:00
parent 9cb05060d3
commit f8f65c991a
40 changed files with 280 additions and 1379 deletions

View File

@ -2,11 +2,9 @@
<library name="jackson">
<CLASSES>
<root url="jar://$GRADLE_REPOSITORY$/org.codehaus.jackson/jackson-core-asl/jars/jackson-core-asl-1.5.2.jar!/" />
<root url="jar://$GRADLE_REPOSITORY$/org.codehaus.jackson/jackson-mapper-asl/jars/jackson-mapper-asl-1.5.2.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="file://$PROJECT_DIR$/../../../opt/jackson/1.5.2/src/mapper/java" />
<root url="file://$PROJECT_DIR$/../../../opt/jackson/1.5.2/src/java" />
</SOURCES>
</library>

View File

@ -1,149 +0,0 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.benchmark.micro.deps.jackson;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.elasticsearch.util.Preconditions;
import org.elasticsearch.util.StopWatch;
import org.elasticsearch.util.io.FastStringReader;
import org.elasticsearch.util.io.Streams;
import org.elasticsearch.util.io.StringBuilderWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import static org.elasticsearch.util.json.Jackson.*;
/**
* A simple Jackson type benchmark to check how well it converts to different types it supports
* such as Map and JsonNode.
*
* @author kimchy (Shay Banon)
*/
@SuppressWarnings({"unchecked"})
public class JacksonTypesBenchmark {
private final String jsonString;
private final int factor;
private final int cycles;
private final ObjectMapper objectMapper;
private final JsonType[] types;
public JacksonTypesBenchmark(String jsonString) throws IOException {
Preconditions.checkNotNull(jsonString, "jsonString must have a value");
this.jsonString = jsonString;
this.objectMapper = defaultObjectMapper();
this.factor = 10;
this.cycles = 10000;
// warm things up
JsonType[] types = buildTypes();
for (JsonType type : types) {
type.runRead(1000);
type.runWrite(1000);
}
this.types = buildTypes();
}
/**
* Runs the test. Will run <tt>factor * cycles</tt> iterations interleaving the
* different type operations by <tt>factor</tt>.
*/
public void run() throws IOException {
// interleave the type tests so GC won't be taken into account
for (int i = 0; i < factor; i++) {
for (JsonType type : types) {
type.runRead(cycles);
type.runWrite(cycles);
}
}
System.out.println("Run [" + (cycles * factor) + "] iterations");
System.out.println("==============================");
for (JsonType type : types) {
System.out.println("------------------------------");
System.out.println("Type [" + type.type.getSimpleName() + "]");
System.out.println(type.readStopWatch.shortSummary());
System.out.println(type.writeStopWatch.shortSummary());
System.out.println("------------------------------");
}
}
/**
* Builds the types that we are going to test.
*/
private JsonType[] buildTypes() throws IOException {
JsonType[] types = new JsonType[2];
types[0] = new JsonType(jsonString, objectMapper, Map.class);
types[1] = new JsonType(jsonString, objectMapper, JsonNode.class);
return types;
}
/**
* Represents a test for a specific type, allowing to runRead and runWrite
* on it and finally getting the results from the write/read stop watches.
*/
private static class JsonType {
final StopWatch readStopWatch = new StopWatch("read").keepTaskList(false);
final StopWatch writeStopWatch = new StopWatch("write").keepTaskList(false);
final String jsonString;
final ObjectMapper objectMapper;
final Class type;
final Object master;
protected JsonType(String jsonString, ObjectMapper objectMapper, Class type) throws IOException {
this.jsonString = jsonString;
this.objectMapper = objectMapper;
this.type = type;
this.master = objectMapper.readValue(new FastStringReader(jsonString), type);
}
void runRead(int cycles) throws IOException {
readStopWatch.start();
for (int i = 0; i < cycles; i++) {
objectMapper.readValue(new FastStringReader(jsonString), type);
}
readStopWatch.stop();
}
void runWrite(int cycles) throws IOException {
writeStopWatch.start();
for (int i = 0; i < cycles; i++) {
StringBuilderWriter builderWriter = StringBuilderWriter.Cached.cached();
objectMapper.writeValue(builderWriter, master);
builderWriter.toString();
}
writeStopWatch.stop();
}
}
public static void main(String[] args) throws Exception {
JacksonTypesBenchmark benchmark = new JacksonTypesBenchmark(
Streams.copyToString(new InputStreamReader(JacksonTypesBenchmark.class.getResourceAsStream("/org/elasticsearch/benchmark/micro/deps/jackson/test1.json"))));
benchmark.run();
}
}

View File

@ -1,22 +0,0 @@
{
glossary: {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}

View File

@ -23,8 +23,8 @@ import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.node.Node;
import org.elasticsearch.util.StopWatch;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
import java.util.Random;
@ -33,8 +33,8 @@ import java.util.concurrent.TimeUnit;
import static org.elasticsearch.client.Requests.*;
import static org.elasticsearch.cluster.metadata.IndexMetaData.*;
import static org.elasticsearch.node.NodeBuilder.*;
import static org.elasticsearch.util.json.JsonBuilder.*;
import static org.elasticsearch.util.settings.ImmutableSettings.*;
import static org.elasticsearch.util.xcontent.XContentFactory.*;
/**
* @author kimchy (Shay Banon)
@ -82,7 +82,7 @@ public class SimpleMemoryMonitorBenchmark {
node2.close();
}
private static JsonBuilder source(String id, String nameValue) throws IOException {
private static XContentBuilder source(String id, String nameValue) throws IOException {
return jsonBuilder().startObject().field("id", id).field("name", nameValue).endObject();
}
}

View File

@ -39,7 +39,6 @@ dependencies {
compile 'joda-time:joda-time:1.6'
compile 'org.codehaus.jackson:jackson-core-asl:1.5.2'
compile 'org.codehaus.jackson:jackson-mapper-asl:1.5.2'
compile 'org.apache.lucene:lucene-core:3.0.1'
compile 'org.apache.lucene:lucene-analyzers:3.0.1'

View File

@ -24,12 +24,14 @@ import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.master.MasterNodeOperationRequest;
import org.elasticsearch.util.TimeValue;
import org.elasticsearch.util.io.FastCharArrayWriter;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.settings.ImmutableSettings;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.XContentFactory;
import org.elasticsearch.util.xcontent.XContentType;
import org.elasticsearch.util.xcontent.builder.TextXContentBuilder;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
import java.util.Map;
@ -38,7 +40,6 @@ import java.util.concurrent.TimeUnit;
import static org.elasticsearch.action.Actions.*;
import static org.elasticsearch.util.TimeValue.*;
import static org.elasticsearch.util.gcommon.collect.Maps.*;
import static org.elasticsearch.util.json.Jackson.*;
import static org.elasticsearch.util.settings.ImmutableSettings.Builder.*;
import static org.elasticsearch.util.settings.ImmutableSettings.*;
@ -139,13 +140,14 @@ public class CreateIndexRequest extends MasterNodeOperationRequest {
* The settings to crete the index with (either json/yaml/properties format)
*/
public CreateIndexRequest settings(Map source) {
FastCharArrayWriter writer = FastCharArrayWriter.Cached.cached();
try {
defaultObjectMapper().writeValue(writer, source);
TextXContentBuilder builder = XContentFactory.contentTextBuilder(XContentType.JSON);
builder.map(source);
settings(builder.string());
} catch (IOException e) {
throw new ElasticSearchGenerationException("Failed to generate [" + source + "]", e);
}
return settings(writer.toString());
return this;
}
/**
@ -173,7 +175,7 @@ public class CreateIndexRequest extends MasterNodeOperationRequest {
* @param type The mapping type
* @param source The mapping source
*/
public CreateIndexRequest mapping(String type, JsonBuilder source) {
public CreateIndexRequest mapping(String type, XContentBuilder source) {
try {
mappings.put(type, source.string());
} catch (IOException e) {
@ -189,13 +191,13 @@ public class CreateIndexRequest extends MasterNodeOperationRequest {
* @param source The mapping source
*/
public CreateIndexRequest mapping(String type, Map source) {
FastCharArrayWriter writer = FastCharArrayWriter.Cached.cached();
try {
defaultObjectMapper().writeValue(writer, source);
TextXContentBuilder builder = XContentFactory.contentTextBuilder(XContentType.JSON);
builder.map(source);
return mapping(type, builder.string());
} catch (IOException e) {
throw new ElasticSearchGenerationException("Failed to generate [" + source + "]", e);
}
return mapping(type, writer.toString());
}
Map<String, String> mappings() {

View File

@ -25,10 +25,12 @@ import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.master.MasterNodeOperationRequest;
import org.elasticsearch.util.Required;
import org.elasticsearch.util.TimeValue;
import org.elasticsearch.util.io.FastCharArrayWriter;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.xcontent.XContentFactory;
import org.elasticsearch.util.xcontent.XContentType;
import org.elasticsearch.util.xcontent.builder.TextXContentBuilder;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
import java.util.Map;
@ -36,7 +38,6 @@ import java.util.concurrent.TimeUnit;
import static org.elasticsearch.action.Actions.*;
import static org.elasticsearch.util.TimeValue.*;
import static org.elasticsearch.util.json.Jackson.*;
/**
* Puts mapping definition registered under a specific type into one or more indices. Best created with
@ -123,7 +124,7 @@ public class PutMappingRequest extends MasterNodeOperationRequest {
/**
* The mapping source definition.
*/
@Required public PutMappingRequest source(JsonBuilder mappingBuilder) {
@Required public PutMappingRequest source(XContentBuilder mappingBuilder) {
try {
return source(mappingBuilder.string());
} catch (IOException e) {
@ -135,13 +136,13 @@ public class PutMappingRequest extends MasterNodeOperationRequest {
* The mapping source definition.
*/
@Required public PutMappingRequest source(Map mappingSource) {
FastCharArrayWriter writer = FastCharArrayWriter.Cached.cached();
try {
defaultObjectMapper().writeValue(writer, mappingSource);
TextXContentBuilder builder = XContentFactory.contentTextBuilder(XContentType.JSON);
builder.map(mappingSource);
return source(builder.string());
} catch (IOException e) {
throw new ElasticSearchGenerationException("Failed to generate [" + mappingSource + "]", e);
}
return source(writer.toString());
}
/**

View File

@ -27,16 +27,17 @@ import org.elasticsearch.util.Required;
import org.elasticsearch.util.Strings;
import org.elasticsearch.util.TimeValue;
import org.elasticsearch.util.Unicode;
import org.elasticsearch.util.io.FastByteArrayOutputStream;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.xcontent.XContentFactory;
import org.elasticsearch.util.xcontent.XContentType;
import org.elasticsearch.util.xcontent.builder.BinaryXContentBuilder;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import static org.elasticsearch.action.Actions.*;
import static org.elasticsearch.util.json.Jackson.*;
/**
* A request to delete all documents that matching a specific query. Best created with
@ -116,13 +117,13 @@ public class DeleteByQueryRequest extends IndicesReplicationOperationRequest {
* The query source to execute in the form of a map.
*/
@Required public DeleteByQueryRequest query(Map querySource) {
FastByteArrayOutputStream os = FastByteArrayOutputStream.Cached.cached();
try {
defaultObjectMapper().writeValue(os, querySource);
BinaryXContentBuilder builder = XContentFactory.contentBinaryBuilder(XContentType.JSON);
builder.map(querySource);
this.querySource = builder.copiedBytes();
} catch (IOException e) {
throw new ElasticSearchGenerationException("Failed to generate [" + querySource + "]", e);
}
this.querySource = os.copiedByteArray();
return this;
}

View File

@ -26,6 +26,8 @@ import org.elasticsearch.util.gcommon.collect.ImmutableMap;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.io.stream.Streamable;
import org.elasticsearch.util.xcontent.XContentFactory;
import org.elasticsearch.util.xcontent.XContentParser;
import java.io.IOException;
import java.util.Iterator;
@ -34,7 +36,6 @@ import java.util.Map;
import static org.elasticsearch.action.get.GetField.*;
import static org.elasticsearch.util.gcommon.collect.Iterators.*;
import static org.elasticsearch.util.gcommon.collect.Maps.*;
import static org.elasticsearch.util.json.Jackson.*;
/**
* The response of a get action.
@ -158,11 +159,18 @@ public class GetResponse implements ActionResponse, Streamable, Iterable<GetFiel
if (sourceAsMap != null) {
return sourceAsMap;
}
XContentParser parser = null;
try {
sourceAsMap = defaultObjectMapper().readValue(source, 0, source.length, Map.class);
parser = XContentFactory.xContent(source).createParser(source);
sourceAsMap = parser.map();
parser.close();
return sourceAsMap;
} catch (Exception e) {
throw new ElasticSearchParseException("Failed to parse source to map", e);
} finally {
if (parser != null) {
parser.close();
}
}
}

View File

@ -26,16 +26,17 @@ import org.elasticsearch.action.support.replication.ShardReplicationOperationReq
import org.elasticsearch.util.Required;
import org.elasticsearch.util.TimeValue;
import org.elasticsearch.util.Unicode;
import org.elasticsearch.util.io.FastByteArrayOutputStream;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.xcontent.XContentFactory;
import org.elasticsearch.util.xcontent.XContentType;
import org.elasticsearch.util.xcontent.builder.BinaryXContentBuilder;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
import java.util.Map;
import static org.elasticsearch.action.Actions.*;
import static org.elasticsearch.util.json.Jackson.*;
/**
* Index request to index a typed JSON document into a specific index and make it searchable. Best
@ -44,9 +45,9 @@ import static org.elasticsearch.util.json.Jackson.*;
* <p>The index requires the {@link #index()}, {@link #type(String)}, {@link #id(String)} and
* {@link #source(byte[])} to be set.
*
* <p>The source (JSON to index) can be set in its bytes form using ({@link #source(byte[])}),
* its string form ({@link #source(String)}) or using a {@link org.elasticsearch.util.json.JsonBuilder}
* ({@link #source(org.elasticsearch.util.json.JsonBuilder)}).
* <p>The source (content to index) can be set in its bytes form using ({@link #source(byte[])}),
* its string form ({@link #source(String)}) or using a {@link org.elasticsearch.util.xcontent.builder.XContentBuilder}
* ({@link #source(org.elasticsearch.util.xcontent.builder.XContentBuilder)}).
*
* <p>If the {@link #id(String)} is not set, it will be automatically generated.
*
@ -209,20 +210,20 @@ public class IndexRequest extends ShardReplicationOperationRequest {
* @param source The map to index
*/
@Required public IndexRequest source(Map source) throws ElasticSearchGenerationException {
FastByteArrayOutputStream os = FastByteArrayOutputStream.Cached.cached();
try {
defaultObjectMapper().writeValue(os, source);
BinaryXContentBuilder builder = XContentFactory.contentBinaryBuilder(XContentType.JSON);
builder.map(source);
this.source = builder.copiedBytes();
} catch (IOException e) {
throw new ElasticSearchGenerationException("Failed to generate [" + source + "]", e);
}
this.source = os.copiedByteArray();
return this;
}
/**
* Sets the JSON source to index.
*
* <p>Note, its preferable to either set it using {@link #source(org.elasticsearch.util.json.JsonBuilder)}
* <p>Note, its preferable to either set it using {@link #source(org.elasticsearch.util.xcontent.builder.XContentBuilder)}
* or using the {@link #source(byte[])}.
*/
@Required public IndexRequest source(String source) {
@ -231,9 +232,9 @@ public class IndexRequest extends ShardReplicationOperationRequest {
}
/**
* Sets the JSON source to index.
* Sets the content source to index.
*/
@Required public IndexRequest source(JsonBuilder jsonBuilder) {
@Required public IndexRequest source(XContentBuilder jsonBuilder) {
try {
return source(jsonBuilder.copiedBytes());
} catch (IOException e) {

View File

@ -31,15 +31,16 @@ import org.elasticsearch.util.Bytes;
import org.elasticsearch.util.Required;
import org.elasticsearch.util.Strings;
import org.elasticsearch.util.Unicode;
import org.elasticsearch.util.io.FastByteArrayOutputStream;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.xcontent.XContentFactory;
import org.elasticsearch.util.xcontent.XContentType;
import org.elasticsearch.util.xcontent.builder.BinaryXContentBuilder;
import java.io.IOException;
import java.util.Map;
import static org.elasticsearch.search.Scroll.*;
import static org.elasticsearch.util.json.Jackson.*;
/**
* A more like this request allowing to search for documents that a "like" the provided document. The document
@ -317,13 +318,13 @@ public class MoreLikeThisRequest implements ActionRequest {
}
public MoreLikeThisRequest searchSource(Map searchSource) {
FastByteArrayOutputStream os = FastByteArrayOutputStream.Cached.cached();
try {
defaultObjectMapper().writeValue(os, searchSource);
BinaryXContentBuilder builder = XContentFactory.contentBinaryBuilder(XContentType.JSON);
builder.map(searchSource);
this.searchSource = builder.copiedBytes();
} catch (IOException e) {
throw new ElasticSearchGenerationException("Failed to generate [" + searchSource + "]", e);
}
this.searchSource = os.copiedByteArray();
return this;
}

View File

@ -29,9 +29,11 @@ import org.elasticsearch.util.Bytes;
import org.elasticsearch.util.Strings;
import org.elasticsearch.util.TimeValue;
import org.elasticsearch.util.Unicode;
import org.elasticsearch.util.io.FastByteArrayOutputStream;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.xcontent.XContentFactory;
import org.elasticsearch.util.xcontent.XContentType;
import org.elasticsearch.util.xcontent.builder.BinaryXContentBuilder;
import java.io.IOException;
import java.util.Map;
@ -39,7 +41,6 @@ import java.util.Map;
import static org.elasticsearch.action.Actions.*;
import static org.elasticsearch.search.Scroll.*;
import static org.elasticsearch.util.TimeValue.*;
import static org.elasticsearch.util.json.Jackson.*;
/**
* A request to execute search against one or more indices (or all). Best created using
@ -202,13 +203,13 @@ public class SearchRequest implements ActionRequest {
* The source of the search request in the form of a map.
*/
public SearchRequest source(Map source) {
FastByteArrayOutputStream os = FastByteArrayOutputStream.Cached.cached();
try {
defaultObjectMapper().writeValue(os, source);
BinaryXContentBuilder builder = XContentFactory.contentBinaryBuilder(XContentType.JSON);
builder.map(source);
this.source = builder.copiedBytes();
} catch (IOException e) {
throw new ElasticSearchGenerationException("Failed to generate [" + source + "]", e);
}
this.source = os.copiedByteArray();
return this;
}
@ -235,13 +236,13 @@ public class SearchRequest implements ActionRequest {
}
public SearchRequest extraSource(Map extraSource) {
FastByteArrayOutputStream os = FastByteArrayOutputStream.Cached.cached();
try {
defaultObjectMapper().writeValue(os, extraSource);
BinaryXContentBuilder builder = XContentFactory.contentBinaryBuilder(XContentType.JSON);
builder.map(extraSource);
this.extraSource = builder.copiedBytes();
} catch (IOException e) {
throw new ElasticSearchGenerationException("Failed to generate [" + extraSource + "]", e);
throw new ElasticSearchGenerationException("Failed to generate [" + source + "]", e);
}
this.extraSource = os.copiedByteArray();
return this;
}

View File

@ -19,8 +19,6 @@
package org.elasticsearch.cluster.metadata;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.elasticsearch.util.MapBuilder;
import org.elasticsearch.util.Preconditions;
import org.elasticsearch.util.concurrent.Immutable;
@ -28,10 +26,11 @@ import org.elasticsearch.util.gcommon.collect.ImmutableMap;
import org.elasticsearch.util.gcommon.collect.ImmutableSet;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.json.ToJson;
import org.elasticsearch.util.settings.ImmutableSettings;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.ToXContent;
import org.elasticsearch.util.xcontent.XContentParser;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import javax.annotation.Nullable;
import java.io.IOException;
@ -202,7 +201,7 @@ public class IndexMetaData {
return new IndexMetaData(index, settings, mappings.immutableMap());
}
public static void toJson(IndexMetaData indexMetaData, JsonBuilder builder, ToJson.Params params) throws IOException {
public static void toXContent(IndexMetaData indexMetaData, XContentBuilder builder, ToXContent.Params params) throws IOException {
builder.startObject(indexMetaData.index());
builder.startObject("settings");
@ -222,33 +221,33 @@ public class IndexMetaData {
builder.endObject();
}
public static IndexMetaData fromJson(JsonParser jp, @Nullable Settings globalSettings) throws IOException {
Builder builder = new Builder(jp.getCurrentName());
public static IndexMetaData fromXContent(XContentParser parser, @Nullable Settings globalSettings) throws IOException {
Builder builder = new Builder(parser.currentName());
String currentFieldName = null;
JsonToken token = jp.nextToken();
while ((token = jp.nextToken()) != JsonToken.END_OBJECT) {
if (token == JsonToken.FIELD_NAME) {
currentFieldName = jp.getCurrentName();
} else if (token == JsonToken.START_OBJECT) {
XContentParser.Token token = parser.nextToken();
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_OBJECT) {
if ("settings".equals(currentFieldName)) {
ImmutableSettings.Builder settingsBuilder = settingsBuilder().globalSettings(globalSettings);
while ((token = jp.nextToken()) != JsonToken.END_OBJECT) {
String key = jp.getCurrentName();
token = jp.nextToken();
String value = jp.getText();
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
String key = parser.currentName();
token = parser.nextToken();
String value = parser.text();
settingsBuilder.put(key, value);
}
builder.settings(settingsBuilder.build());
} else if ("mappings".equals(currentFieldName)) {
while ((token = jp.nextToken()) != JsonToken.END_OBJECT) {
String mappingType = jp.getCurrentName();
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
String mappingType = parser.currentName();
String mappingSource = null;
while ((token = jp.nextToken()) != JsonToken.END_OBJECT) {
if (token == JsonToken.FIELD_NAME) {
if ("source".equals(jp.getCurrentName())) {
jp.nextToken();
mappingSource = jp.getText();
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
if ("source".equals(parser.currentName())) {
parser.nextToken();
mappingSource = parser.text();
}
}
}

View File

@ -19,8 +19,6 @@
package org.elasticsearch.cluster.metadata;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.index.Index;
import org.elasticsearch.indices.IndexMissingException;
@ -32,10 +30,13 @@ import org.elasticsearch.util.gcommon.collect.Lists;
import org.elasticsearch.util.gcommon.collect.UnmodifiableIterator;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.json.StringJsonBuilder;
import org.elasticsearch.util.json.ToJson;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.ToXContent;
import org.elasticsearch.util.xcontent.XContentFactory;
import org.elasticsearch.util.xcontent.XContentParser;
import org.elasticsearch.util.xcontent.XContentType;
import org.elasticsearch.util.xcontent.builder.TextXContentBuilder;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import javax.annotation.Nullable;
import java.io.IOException;
@ -270,44 +271,48 @@ public class MetaData implements Iterable<IndexMetaData> {
return new MetaData(indices.immutableMap(), maxNumberOfShardsPerNode);
}
public static String toJson(MetaData metaData) throws IOException {
StringJsonBuilder builder = JsonBuilder.stringJsonBuilder().prettyPrint();
public static String toXContent(MetaData metaData) throws IOException {
TextXContentBuilder builder = XContentFactory.contentTextBuilder(XContentType.JSON);
builder.startObject();
toJson(metaData, builder, ToJson.EMPTY_PARAMS);
toXContent(metaData, builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
return builder.string();
}
public static void toJson(MetaData metaData, JsonBuilder builder, ToJson.Params params) throws IOException {
public static void toXContent(MetaData metaData, XContentBuilder builder, ToXContent.Params params) throws IOException {
builder.startObject("meta-data");
builder.field("max_number_of_shards_per_node", metaData.maxNumberOfShardsPerNode());
builder.startObject("indices");
for (IndexMetaData indexMetaData : metaData) {
IndexMetaData.Builder.toJson(indexMetaData, builder, params);
IndexMetaData.Builder.toXContent(indexMetaData, builder, params);
}
builder.endObject();
builder.endObject();
}
public static MetaData fromJson(JsonParser jp, @Nullable Settings globalSettings) throws IOException {
public static MetaData fromXContent(XContentParser parser, @Nullable Settings globalSettings) throws IOException {
Builder builder = new Builder();
String currentFieldName = null;
JsonToken token = jp.nextToken();
while ((token = jp.nextToken()) != JsonToken.END_OBJECT) {
if (token == JsonToken.FIELD_NAME) {
currentFieldName = jp.getCurrentName();
} else if (token == JsonToken.START_OBJECT) {
XContentParser.Token token = parser.nextToken();
if (token == null) {
// no data...
return builder.build();
}
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_OBJECT) {
if ("indices".equals(currentFieldName)) {
while ((token = jp.nextToken()) != JsonToken.END_OBJECT) {
builder.put(IndexMetaData.Builder.fromJson(jp, globalSettings));
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
builder.put(IndexMetaData.Builder.fromXContent(parser, globalSettings));
}
}
} else if (token == JsonToken.VALUE_NUMBER_INT) {
} else if (token.isValue()) {
if ("max_number_of_shards_per_node".equals(currentFieldName)) {
builder.maxNumberOfShardsPerNode(jp.getIntValue());
builder.maxNumberOfShardsPerNode(parser.intValue());
}
}
}

View File

@ -19,10 +19,6 @@
package org.elasticsearch.gateway.fs;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.util.guice.inject.Module;
import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonParser;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.metadata.MetaData;
@ -31,12 +27,15 @@ import org.elasticsearch.gateway.Gateway;
import org.elasticsearch.gateway.GatewayException;
import org.elasticsearch.index.gateway.fs.FsIndexGatewayModule;
import org.elasticsearch.util.component.AbstractLifecycleComponent;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.util.guice.inject.Module;
import org.elasticsearch.util.io.FileSystemUtils;
import org.elasticsearch.util.json.BinaryJsonBuilder;
import org.elasticsearch.util.json.Jackson;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.json.ToJson;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.ToXContent;
import org.elasticsearch.util.xcontent.XContentFactory;
import org.elasticsearch.util.xcontent.XContentParser;
import org.elasticsearch.util.xcontent.XContentType;
import org.elasticsearch.util.xcontent.builder.BinaryXContentBuilder;
import java.io.*;
@ -122,15 +121,14 @@ public class FsGateway extends AbstractLifecycleComponent<Gateway> implements Ga
throw new GatewayException("Failed to create new file [" + file + "]");
}
FileOutputStream fileStream = new FileOutputStream(file);
JsonBuilder builder = new BinaryJsonBuilder(Jackson.defaultJsonFactory().createJsonGenerator(fileStream, JsonEncoding.UTF8));
BinaryXContentBuilder builder = XContentFactory.contentBinaryBuilder(XContentType.JSON);
builder.prettyPrint();
builder.startObject();
MetaData.Builder.toJson(metaData, builder, ToJson.EMPTY_PARAMS);
MetaData.Builder.toXContent(metaData, builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
builder.close();
FileOutputStream fileStream = new FileOutputStream(file);
fileStream.write(builder.unsafeBytes(), 0, builder.unsafeBytesLength());
fileStream.close();
syncFile(file);
@ -209,17 +207,13 @@ public class FsGateway extends AbstractLifecycleComponent<Gateway> implements Ga
private MetaData readMetaData(File file) throws IOException {
FileInputStream fileStream = new FileInputStream(file);
JsonParser jp = null;
XContentParser parser = null;
try {
jp = Jackson.defaultJsonFactory().createJsonParser(fileStream);
return MetaData.Builder.fromJson(jp, settings);
parser = XContentFactory.xContent(XContentType.JSON).createParser(fileStream);
return MetaData.Builder.fromXContent(parser, settings);
} finally {
if (jp != null) {
try {
jp.close();
} catch (Exception e) {
// ignore
}
if (parser != null) {
parser.close();
}
try {
fileStream.close();

View File

@ -19,8 +19,6 @@
package org.elasticsearch.rest.action.admin.indices.alias;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
@ -29,8 +27,9 @@ import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.metadata.AliasAction;
import org.elasticsearch.rest.*;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.util.json.Jackson;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.XContentFactory;
import org.elasticsearch.util.xcontent.XContentParser;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
@ -58,16 +57,17 @@ public class RestIndicesAliasesAction extends BaseRestHandler {
// { remove : { index : "test1", alias : "alias1" } }
// ]
// }
JsonParser jp = Jackson.defaultJsonFactory().createJsonParser(request.contentAsStream());
JsonToken token = jp.nextToken();
byte[] content = request.contentAsBytes();
XContentParser parser = XContentFactory.xContent(content).createParser(content);
XContentParser.Token token = parser.nextToken();
if (token == null) {
throw new ElasticSearchIllegalArgumentException("No action is specified");
}
while ((token = jp.nextToken()) != JsonToken.END_OBJECT) {
if (token == JsonToken.START_ARRAY) {
while ((token = jp.nextToken()) != JsonToken.END_ARRAY) {
if (token == JsonToken.FIELD_NAME) {
String action = jp.getCurrentName();
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.START_ARRAY) {
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
if (token == XContentParser.Token.FIELD_NAME) {
String action = parser.currentName();
AliasAction.Type type;
if ("add".equals(action)) {
type = AliasAction.Type.ADD;
@ -79,14 +79,14 @@ public class RestIndicesAliasesAction extends BaseRestHandler {
String index = null;
String alias = null;
String currentFieldName = null;
while ((token = jp.nextToken()) != JsonToken.END_OBJECT) {
if (token == JsonToken.FIELD_NAME) {
currentFieldName = jp.getCurrentName();
} else if (token == JsonToken.VALUE_STRING) {
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.VALUE_STRING) {
if ("index".equals(currentFieldName)) {
index = jp.getText();
index = parser.text();
} else if ("alias".equals(currentFieldName)) {
alias = jp.getText();
alias = parser.text();
}
}
}

View File

@ -19,21 +19,22 @@
package org.elasticsearch.rest.action.main;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.node.ArrayNode;
import org.elasticsearch.Version;
import org.elasticsearch.client.Client;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.util.Classes;
import org.elasticsearch.util.concurrent.jsr166y.ThreadLocalRandom;
import org.elasticsearch.util.gcommon.collect.Iterators;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.util.json.Jackson;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.XContentFactory;
import org.elasticsearch.util.xcontent.XContentParser;
import org.elasticsearch.util.xcontent.XContentType;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import static org.elasticsearch.rest.RestRequest.Method.*;
@ -42,18 +43,19 @@ import static org.elasticsearch.rest.RestRequest.Method.*;
*/
public class RestMainAction extends BaseRestHandler {
private final JsonNode rootNode;
private final Map<String, Object> rootNode;
private final int quotesSize;
@Inject public RestMainAction(Settings settings, Client client, RestController controller) {
super(settings, client);
JsonNode rootNode;
Map<String, Object> rootNode;
int quotesSize;
try {
rootNode = Jackson.defaultObjectMapper().readValue(Classes.getDefaultClassLoader().getResourceAsStream("org/elasticsearch/rest/action/main/quotes.json"), JsonNode.class);
ArrayNode arrayNode = (ArrayNode) rootNode.get("quotes");
quotesSize = Iterators.size(arrayNode.getElements());
XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(Classes.getDefaultClassLoader().getResourceAsStream("org/elasticsearch/rest/action/main/quotes.json"));
rootNode = parser.map();
List arrayNode = (List) rootNode.get("quotes");
quotesSize = arrayNode.size();
} catch (Exception e) {
rootNode = null;
quotesSize = -1;
@ -73,24 +75,23 @@ public class RestMainAction extends BaseRestHandler {
builder.field("name", settings.get("name"));
}
builder.startObject("version").field("number", Version.number()).field("date", Version.date()).field("snapshot_build", Version.snapshotBuild()).endObject();
builder.field("version", Version.number());
builder.field("tagline", "You Know, for Search");
builder.field("cover", "DON'T PANIC");
if (rootNode != null) {
builder.startObject("quote");
ArrayNode arrayNode = (ArrayNode) rootNode.get("quotes");
JsonNode quoteNode = arrayNode.get(ThreadLocalRandom.current().nextInt(quotesSize));
builder.field("book", quoteNode.get("book").getValueAsText());
builder.field("chapter", quoteNode.get("chapter").getValueAsText());
ArrayNode textNodes = (ArrayNode) quoteNode.get("text");
List arrayNode = (List) rootNode.get("quotes");
Map<String, Object> quoteNode = (Map<String, Object>) arrayNode.get(ThreadLocalRandom.current().nextInt(quotesSize));
builder.field("book", quoteNode.get("book").toString());
builder.field("chapter", quoteNode.get("chapter").toString());
List textNodes = (List) quoteNode.get("text");
// builder.startArray("text");
// for (JsonNode textNode : textNodes) {
// builder.value(textNode.getValueAsText());
// }
// builder.endArray();
int index = 0;
for (JsonNode textNode : textNodes) {
builder.field("text" + (++index), textNode.getValueAsText());
for (Object textNode : textNodes) {
builder.field("text" + (++index), textNode.toString());
}
builder.endObject();
}

View File

@ -31,6 +31,7 @@ import org.elasticsearch.util.gnu.trove.TIntObjectHashMap;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.xcontent.XContentFactory;
import org.elasticsearch.util.xcontent.XContentParser;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import javax.annotation.Nullable;
@ -41,7 +42,6 @@ import java.util.Map;
import static org.elasticsearch.search.SearchShardTarget.*;
import static org.elasticsearch.search.highlight.HighlightField.*;
import static org.elasticsearch.search.internal.InternalSearchHitField.*;
import static org.elasticsearch.util.json.Jackson.*;
import static org.elasticsearch.util.lucene.Lucene.*;
/**
@ -130,11 +130,18 @@ public class InternalSearchHit implements SearchHit {
if (sourceAsMap != null) {
return sourceAsMap;
}
XContentParser parser = null;
try {
sourceAsMap = defaultObjectMapper().readValue(source, 0, source.length, Map.class);
parser = XContentFactory.xContent(source).createParser(source);
sourceAsMap = parser.map();
parser.close();
return sourceAsMap;
} catch (Exception e) {
throw new ElasticSearchParseException("Failed to parse source to map", e);
} finally {
if (parser != null) {
parser.close();
}
}
}

View File

@ -1,123 +0,0 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.util.json;
import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.util.ThreadLocals;
import org.elasticsearch.util.Unicode;
import org.elasticsearch.util.io.FastByteArrayOutputStream;
import java.io.IOException;
/**
* @author kimchy (shay.banon)
*/
public class BinaryJsonBuilder extends JsonBuilder<BinaryJsonBuilder> {
/**
* A thread local based cache of {@link BinaryJsonBuilder}.
*/
public static class Cached {
private static final ThreadLocal<ThreadLocals.CleanableValue<BinaryJsonBuilder>> cache = new ThreadLocal<ThreadLocals.CleanableValue<BinaryJsonBuilder>>() {
@Override protected ThreadLocals.CleanableValue<BinaryJsonBuilder> initialValue() {
try {
BinaryJsonBuilder builder = new BinaryJsonBuilder();
builder.cachedStringBuilder = new StringBuilder();
return new ThreadLocals.CleanableValue<BinaryJsonBuilder>(builder);
} catch (IOException e) {
throw new ElasticSearchException("Failed to create json generator", e);
}
}
};
/**
* Returns the cached thread local generator, with its internal {@link StringBuilder} cleared.
*/
static BinaryJsonBuilder cached() throws IOException {
ThreadLocals.CleanableValue<BinaryJsonBuilder> cached = cache.get();
cached.get().reset();
return cached.get();
}
}
private final FastByteArrayOutputStream bos;
private final JsonFactory factory;
public BinaryJsonBuilder() throws IOException {
this(Jackson.defaultJsonFactory());
}
public BinaryJsonBuilder(JsonFactory factory) throws IOException {
this.bos = new FastByteArrayOutputStream();
this.factory = factory;
this.generator = factory.createJsonGenerator(bos, JsonEncoding.UTF8);
this.builder = this;
}
public BinaryJsonBuilder(JsonGenerator generator) throws IOException {
this.bos = null;
this.generator = generator;
this.factory = null;
this.builder = this;
}
@Override public BinaryJsonBuilder raw(byte[] json) throws IOException {
flush();
bos.write(json);
return this;
}
@Override public BinaryJsonBuilder reset() throws IOException {
fieldCaseConversion = globalFieldCaseConversion;
bos.reset();
generator = factory.createJsonGenerator(bos, JsonEncoding.UTF8);
return this;
}
public FastByteArrayOutputStream unsafeStream() throws IOException {
flush();
return bos;
}
@Override public byte[] unsafeBytes() throws IOException {
flush();
return bos.unsafeByteArray();
}
@Override public int unsafeBytesLength() throws IOException {
flush();
return bos.size();
}
@Override public byte[] copiedBytes() throws IOException {
flush();
return bos.copiedByteArray();
}
@Override public String string() throws IOException {
flush();
return Unicode.fromBytes(bos.unsafeByteArray(), 0, bos.size());
}
}

View File

@ -1,159 +0,0 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.util.json;
import org.codehaus.jackson.*;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.deser.CustomDeserializerFactory;
import org.codehaus.jackson.map.deser.StdDeserializer;
import org.codehaus.jackson.map.deser.StdDeserializerProvider;
import org.codehaus.jackson.map.ser.CustomSerializerFactory;
import org.codehaus.jackson.map.ser.SerializerBase;
import org.elasticsearch.util.joda.FormatDateTimeFormatter;
import org.elasticsearch.util.joda.Joda;
import org.joda.time.DateTime;
import java.io.IOException;
import java.util.Date;
/**
* A set of helper methods for Jackson.
*
* @author kimchy (Shay Banon)
*/
public final class Jackson {
private static final JsonFactory defaultJsonFactory;
private static final ObjectMapper defaultObjectMapper;
static {
defaultJsonFactory = newJsonFactory();
defaultObjectMapper = newObjectMapper();
}
public static JsonFactory defaultJsonFactory() {
return defaultJsonFactory;
}
public static ObjectMapper defaultObjectMapper() {
return defaultObjectMapper;
}
public static JsonFactory newJsonFactory() {
JsonFactory jsonFactory = new JsonFactory();
jsonFactory.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
jsonFactory.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, true);
return jsonFactory;
}
public static ObjectMapper newObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
mapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, true);
CustomSerializerFactory serializerFactory = new CustomSerializerFactory();
serializerFactory.addSpecificMapping(Date.class, new DateSerializer());
serializerFactory.addSpecificMapping(DateTime.class, new DateTimeSerializer());
mapper.setSerializerFactory(serializerFactory);
CustomDeserializerFactory deserializerFactory = new CustomDeserializerFactory();
deserializerFactory.addSpecificMapping(Date.class, new DateDeserializer());
deserializerFactory.addSpecificMapping(DateTime.class, new DateTimeDeserializer());
mapper.setDeserializerProvider(new StdDeserializerProvider(deserializerFactory));
return mapper;
}
private Jackson() {
}
public static class DateDeserializer extends StdDeserializer<Date> {
private final FormatDateTimeFormatter formatter = Joda.forPattern("dateTime");
public DateDeserializer() {
super(Date.class);
}
@Override public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonToken t = jp.getCurrentToken();
if (t == JsonToken.VALUE_STRING) {
return new Date(formatter.parser().parseMillis(jp.getText()));
}
throw ctxt.mappingException(getValueClass());
}
}
public final static class DateSerializer extends SerializerBase<Date> {
private final FormatDateTimeFormatter formatter = Joda.forPattern("dateTime");
public DateSerializer() {
super(Date.class);
}
@Override public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
jgen.writeString(formatter.parser().print(value.getTime()));
}
@Override public JsonNode getSchema(SerializerProvider provider, java.lang.reflect.Type typeHint) {
return createSchemaNode("string", true);
}
}
public static class DateTimeDeserializer extends StdDeserializer<DateTime> {
private final FormatDateTimeFormatter formatter = Joda.forPattern("dateTime");
public DateTimeDeserializer() {
super(DateTime.class);
}
@Override public DateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonToken t = jp.getCurrentToken();
if (t == JsonToken.VALUE_STRING) {
return formatter.parser().parseDateTime(jp.getText());
}
throw ctxt.mappingException(getValueClass());
}
}
public final static class DateTimeSerializer extends SerializerBase<DateTime> {
private final FormatDateTimeFormatter formatter = Joda.forPattern("dateTime");
public DateTimeSerializer() {
super(DateTime.class);
}
@Override public void serialize(DateTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
jgen.writeString(formatter.printer().print(value));
}
@Override public JsonNode getSchema(SerializerProvider provider, java.lang.reflect.Type typeHint) {
return createSchemaNode("string", true);
}
}
}

View File

@ -1,415 +0,0 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.util.json;
import org.elasticsearch.util.Strings;
import org.elasticsearch.util.concurrent.NotThreadSafe;
import org.joda.time.DateTimeZone;
import org.joda.time.ReadableInstant;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import java.io.IOException;
import java.util.Date;
/**
* A helper builder for JSON documents.
*
* <p>Best constructed using {@link #stringJsonBuilder()} or {@link #binaryJsonBuilder()}. When used to create
* source for actions/operations, it is recommended to use {@link #binaryJsonBuilder()}.
*
* @author kimchy (shay.banon)
*/
@NotThreadSafe
public abstract class JsonBuilder<T extends JsonBuilder> {
public static enum FieldCaseConversion {
/**
* No came conversion will occur.
*/
NONE,
/**
* Camel Case will be converted to Underscore casing.
*/
UNDERSCORE,
/**
* Underscore will be converted to Camel case conversion.
*/
CAMELCASE
}
private final static DateTimeFormatter defaultDatePrinter = ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC);
protected static FieldCaseConversion globalFieldCaseConversion = FieldCaseConversion.NONE;
public static void globalFieldCaseConversion(FieldCaseConversion globalFieldCaseConversion) {
JsonBuilder.globalFieldCaseConversion = globalFieldCaseConversion;
}
protected org.codehaus.jackson.JsonGenerator generator;
protected T builder;
protected FieldCaseConversion fieldCaseConversion = globalFieldCaseConversion;
protected StringBuilder cachedStringBuilder;
public static StringJsonBuilder stringJsonBuilder() throws IOException {
return StringJsonBuilder.Cached.cached();
}
public static BinaryJsonBuilder jsonBuilder() throws IOException {
return BinaryJsonBuilder.Cached.cached();
}
public static BinaryJsonBuilder binaryJsonBuilder() throws IOException {
return BinaryJsonBuilder.Cached.cached();
}
public T fieldCaseConversion(FieldCaseConversion fieldCaseConversion) {
this.fieldCaseConversion = fieldCaseConversion;
return builder;
}
public T prettyPrint() {
generator.useDefaultPrettyPrinter();
return builder;
}
public T startObject(String name) throws IOException {
field(name);
startObject();
return builder;
}
public T startObject() throws IOException {
generator.writeStartObject();
return builder;
}
public T endObject() throws IOException {
generator.writeEndObject();
return builder;
}
public T array(String name, String... values) throws IOException {
startArray(name);
for (String value : values) {
value(value);
}
endArray();
return builder;
}
public T array(String name, Object... values) throws IOException {
startArray(name);
for (Object value : values) {
value(value);
}
endArray();
return builder;
}
public T startArray(String name) throws IOException {
field(name);
startArray();
return builder;
}
public T startArray() throws IOException {
generator.writeStartArray();
return builder;
}
public T endArray() throws IOException {
generator.writeEndArray();
return builder;
}
public T field(String name) throws IOException {
if (fieldCaseConversion == FieldCaseConversion.UNDERSCORE) {
name = Strings.toUnderscoreCase(name, cachedStringBuilder);
} else if (fieldCaseConversion == FieldCaseConversion.CAMELCASE) {
name = Strings.toCamelCase(name, cachedStringBuilder);
}
generator.writeFieldName(name);
return builder;
}
public T field(String name, char[] value, int offset, int length) throws IOException {
field(name);
if (value == null) {
generator.writeNull();
} else {
generator.writeString(value, offset, length);
}
return builder;
}
public T field(String name, String value) throws IOException {
field(name);
if (value == null) {
generator.writeNull();
} else {
generator.writeString(value);
}
return builder;
}
public T field(String name, Integer value) throws IOException {
return field(name, value.intValue());
}
public T field(String name, int value) throws IOException {
field(name);
generator.writeNumber(value);
return builder;
}
public T field(String name, Long value) throws IOException {
return field(name, value.longValue());
}
public T field(String name, long value) throws IOException {
field(name);
generator.writeNumber(value);
return builder;
}
public T field(String name, Float value) throws IOException {
return field(name, value.floatValue());
}
public T field(String name, float value) throws IOException {
field(name);
generator.writeNumber(value);
return builder;
}
public T field(String name, Double value) throws IOException {
return field(name, value.doubleValue());
}
public T field(String name, double value) throws IOException {
field(name);
generator.writeNumber(value);
return builder;
}
public T field(String name, Object value) throws IOException {
if (value == null) {
nullField(name);
return builder;
}
Class type = value.getClass();
if (type == String.class) {
field(name, (String) value);
} else if (type == Float.class) {
field(name, ((Float) value).floatValue());
} else if (type == Double.class) {
field(name, ((Double) value).doubleValue());
} else if (type == Integer.class) {
field(name, ((Integer) value).intValue());
} else if (type == Long.class) {
field(name, ((Long) value).longValue());
} else if (type == Boolean.class) {
field(name, ((Boolean) value).booleanValue());
} else if (type == Date.class) {
field(name, (Date) value);
} else if (type == byte[].class) {
field(name, (byte[]) value);
} else if (value instanceof ReadableInstant) {
field(name, (ReadableInstant) value);
} else {
field(name, value.toString());
}
return builder;
}
public T field(String name, boolean value) throws IOException {
field(name);
generator.writeBoolean(value);
return builder;
}
public T field(String name, byte[] value) throws IOException {
field(name);
generator.writeBinary(value);
return builder;
}
public T field(String name, ReadableInstant date) throws IOException {
field(name);
return value(date);
}
public T field(String name, ReadableInstant date, DateTimeFormatter formatter) throws IOException {
field(name);
return value(date, formatter);
}
public T field(String name, Date date) throws IOException {
field(name);
return value(date);
}
public T field(String name, Date date, DateTimeFormatter formatter) throws IOException {
field(name);
return value(date, formatter);
}
public T nullField(String name) throws IOException {
generator.writeNullField(name);
return builder;
}
public T nullValue() throws IOException {
generator.writeNull();
return builder;
}
public T raw(String json) throws IOException {
generator.writeRaw(json);
return builder;
}
public abstract T raw(byte[] json) throws IOException;
public T value(Boolean value) throws IOException {
return value(value.booleanValue());
}
public T value(boolean value) throws IOException {
generator.writeBoolean(value);
return builder;
}
public T value(ReadableInstant date) throws IOException {
return value(date, defaultDatePrinter);
}
public T value(ReadableInstant date, DateTimeFormatter dateTimeFormatter) throws IOException {
return value(dateTimeFormatter.print(date));
}
public T value(Date date) throws IOException {
return value(date, defaultDatePrinter);
}
public T value(Date date, DateTimeFormatter dateTimeFormatter) throws IOException {
return value(dateTimeFormatter.print(date.getTime()));
}
public T value(Integer value) throws IOException {
return value(value.intValue());
}
public T value(int value) throws IOException {
generator.writeNumber(value);
return builder;
}
public T value(Long value) throws IOException {
return value(value.longValue());
}
public T value(long value) throws IOException {
generator.writeNumber(value);
return builder;
}
public T value(Float value) throws IOException {
return value(value.floatValue());
}
public T value(float value) throws IOException {
generator.writeNumber(value);
return builder;
}
public T value(Double value) throws IOException {
return value(value.doubleValue());
}
public T value(double value) throws IOException {
generator.writeNumber(value);
return builder;
}
public T value(String value) throws IOException {
generator.writeString(value);
return builder;
}
public T value(byte[] value) throws IOException {
generator.writeBinary(value);
return builder;
}
public T value(Object value) throws IOException {
Class type = value.getClass();
if (type == String.class) {
value((String) value);
} else if (type == Float.class) {
value(((Float) value).floatValue());
} else if (type == Double.class) {
value(((Double) value).doubleValue());
} else if (type == Integer.class) {
value(((Integer) value).intValue());
} else if (type == Long.class) {
value(((Long) value).longValue());
} else if (type == Boolean.class) {
value((Boolean) value);
} else if (type == byte[].class) {
value((byte[]) value);
} else if (type == Date.class) {
value((Date) value);
} else if (value instanceof ReadableInstant) {
value((ReadableInstant) value);
} else {
throw new IOException("Type not allowed [" + type + "]");
}
return builder;
}
public T flush() throws IOException {
generator.flush();
return builder;
}
public abstract T reset() throws IOException;
public abstract byte[] unsafeBytes() throws IOException;
public abstract int unsafeBytesLength() throws IOException;
public abstract byte[] copiedBytes() throws IOException;
public abstract String string() throws IOException;
public void close() {
try {
generator.close();
} catch (IOException e) {
// ignore
}
}
}

View File

@ -1,167 +0,0 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.util.json;
import org.apache.lucene.util.UnicodeUtil;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.util.ThreadLocals;
import org.elasticsearch.util.Unicode;
import org.elasticsearch.util.concurrent.NotThreadSafe;
import org.elasticsearch.util.io.FastCharArrayWriter;
import java.io.IOException;
/**
* @author kimchy (Shay Banon)
*/
@NotThreadSafe
public class StringJsonBuilder extends JsonBuilder<StringJsonBuilder> {
/**
* A thread local based cache of {@link StringJsonBuilder}.
*/
public static class Cached {
private static final ThreadLocal<ThreadLocals.CleanableValue<StringJsonBuilder>> cache = new ThreadLocal<ThreadLocals.CleanableValue<StringJsonBuilder>>() {
@Override protected ThreadLocals.CleanableValue<StringJsonBuilder> initialValue() {
try {
StringJsonBuilder builder = new StringJsonBuilder();
builder.cachedStringBuilder = new StringBuilder();
return new ThreadLocals.CleanableValue<StringJsonBuilder>(builder);
} catch (IOException e) {
throw new ElasticSearchException("Failed to create json generator", e);
}
}
};
/**
* Returns the cached thread local generator, with its internal {@link StringBuilder} cleared.
*/
static StringJsonBuilder cached() throws IOException {
StringJsonBuilder sb = cache.get().get();
sb.reset();
return sb;
}
}
private final FastCharArrayWriter writer;
private final JsonFactory factory;
final UnicodeUtil.UTF8Result utf8Result = new UnicodeUtil.UTF8Result();
public StringJsonBuilder() throws IOException {
this(Jackson.defaultJsonFactory());
}
public StringJsonBuilder(JsonFactory factory) throws IOException {
this.writer = new FastCharArrayWriter();
this.factory = factory;
this.generator = factory.createJsonGenerator(writer);
this.builder = this;
}
public StringJsonBuilder(JsonGenerator generator) throws IOException {
this.writer = new FastCharArrayWriter();
this.generator = generator;
this.factory = null;
this.builder = this;
}
@Override public StringJsonBuilder raw(byte[] json) throws IOException {
flush();
Unicode.UTF16Result result = Unicode.unsafeFromBytesAsUtf16(json);
writer.write(result.result, 0, result.length);
return this;
}
public StringJsonBuilder reset() throws IOException {
fieldCaseConversion = globalFieldCaseConversion;
writer.reset();
generator = factory.createJsonGenerator(writer);
return this;
}
public String string() throws IOException {
flush();
return writer.toStringTrim();
}
public FastCharArrayWriter unsafeChars() throws IOException {
flush();
return writer;
}
@Override public byte[] unsafeBytes() throws IOException {
return utf8().result;
}
/**
* Call this AFTER {@link #unsafeBytes()}.
*/
@Override public int unsafeBytesLength() {
return utf8Result.length;
}
@Override public byte[] copiedBytes() throws IOException {
flush();
byte[] ret = new byte[utf8Result.length];
System.arraycopy(utf8Result.result, 0, ret, 0, ret.length);
return ret;
}
/**
* Returns the byte[] that represents the utf8 of the json written up until now.
* Note, the result is shared within this instance, so copy the byte array if needed
* or use {@link #utf8copied()}.
*/
public UnicodeUtil.UTF8Result utf8() throws IOException {
flush();
// ignore whitepsaces
int st = 0;
int len = writer.size();
char[] val = writer.unsafeCharArray();
while ((st < len) && (val[st] <= ' ')) {
st++;
len--;
}
while ((st < len) && (val[len - 1] <= ' ')) {
len--;
}
UnicodeUtil.UTF16toUTF8(val, st, len, utf8Result);
return utf8Result;
}
/**
* Returns a copied byte[] that represnts the utf8 o fthe json written up until now.
*/
public byte[] utf8copied() throws IOException {
utf8();
byte[] result = new byte[utf8Result.length];
System.arraycopy(utf8Result.result, 0, result, 0, utf8Result.length);
return result;
}
}

View File

@ -1,54 +0,0 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.util.json;
import java.io.IOException;
import java.util.Map;
/**
* @author kimchy (Shay Banon)
*/
public interface ToJson {
public static interface Params {
String param(String key);
}
public static final Params EMPTY_PARAMS = new Params() {
@Override public String param(String key) {
return null;
}
};
public static class MapParams implements Params {
private final Map<String, String> params;
public MapParams(Map<String, String> params) {
this.params = params;
}
@Override public String param(String key) {
return params.get(key);
}
}
void toJson(JsonBuilder builder, Params params) throws IOException;
}

View File

@ -19,12 +19,9 @@
package org.elasticsearch.util.settings.loader;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.elasticsearch.util.io.FastByteArrayInputStream;
import org.elasticsearch.util.io.FastStringReader;
import org.elasticsearch.util.json.Jackson;
import org.elasticsearch.util.xcontent.XContentFactory;
import org.elasticsearch.util.xcontent.XContentParser;
import org.elasticsearch.util.xcontent.XContentType;
import java.io.IOException;
import java.util.List;
@ -37,31 +34,29 @@ import static org.elasticsearch.util.gcommon.collect.Maps.*;
* Settings loader that loads (parses) the settings in a json format by flattening them
* into a map.
*
* @author kimchy (Shay Banon)
* @author kimchy (shay.banon)
*/
public class JsonSettingsLoader implements SettingsLoader {
private final JsonFactory jsonFactory = Jackson.defaultJsonFactory();
@Override public Map<String, String> load(String source) throws IOException {
JsonParser jp = jsonFactory.createJsonParser(new FastStringReader(source));
XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(source);
try {
return load(jp);
return load(parser);
} finally {
jp.close();
parser.close();
}
}
@Override public Map<String, String> load(byte[] source) throws IOException {
JsonParser jp = jsonFactory.createJsonParser(new FastByteArrayInputStream(source));
XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(source);
try {
return load(jp);
return load(parser);
} finally {
jp.close();
parser.close();
}
}
public Map<String, String> load(JsonParser jp) throws IOException {
public Map<String, String> load(XContentParser jp) throws IOException {
StringBuilder sb = new StringBuilder();
Map<String, String> settings = newHashMap();
List<String> path = newArrayList();
@ -70,24 +65,24 @@ public class JsonSettingsLoader implements SettingsLoader {
return settings;
}
private void serializeObject(Map<String, String> settings, StringBuilder sb, List<String> path, JsonParser jp, String objFieldName) throws IOException {
private void serializeObject(Map<String, String> settings, StringBuilder sb, List<String> path, XContentParser parser, String objFieldName) throws IOException {
if (objFieldName != null) {
path.add(objFieldName);
}
String currentFieldName = null;
JsonToken token;
while ((token = jp.nextToken()) != JsonToken.END_OBJECT) {
if (token == JsonToken.START_OBJECT) {
serializeObject(settings, sb, path, jp, currentFieldName);
} else if (token == JsonToken.START_ARRAY) {
serializeArray(settings, sb, path, jp, currentFieldName);
} else if (token == JsonToken.FIELD_NAME) {
currentFieldName = jp.getCurrentName();
} else if (token == JsonToken.VALUE_NULL) {
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.START_OBJECT) {
serializeObject(settings, sb, path, parser, currentFieldName);
} else if (token == XContentParser.Token.START_ARRAY) {
serializeArray(settings, sb, path, parser, currentFieldName);
} else if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.VALUE_NULL) {
// ignore this
} else {
serializeValue(settings, sb, path, jp, currentFieldName);
serializeValue(settings, sb, path, parser, currentFieldName);
}
}
@ -97,31 +92,31 @@ public class JsonSettingsLoader implements SettingsLoader {
}
}
private void serializeArray(Map<String, String> settings, StringBuilder sb, List<String> path, JsonParser jp, String fieldName) throws IOException {
JsonToken token;
private void serializeArray(Map<String, String> settings, StringBuilder sb, List<String> path, XContentParser parser, String fieldName) throws IOException {
XContentParser.Token token;
int counter = 0;
while ((token = jp.nextToken()) != JsonToken.END_ARRAY) {
if (token == JsonToken.START_OBJECT) {
serializeObject(settings, sb, path, jp, fieldName + '.' + (counter++));
} else if (token == JsonToken.START_ARRAY) {
serializeArray(settings, sb, path, jp, fieldName + '.' + (counter++));
} else if (token == JsonToken.FIELD_NAME) {
fieldName = jp.getCurrentName();
} else if (token == JsonToken.VALUE_NULL) {
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
if (token == XContentParser.Token.START_OBJECT) {
serializeObject(settings, sb, path, parser, fieldName + '.' + (counter++));
} else if (token == XContentParser.Token.START_ARRAY) {
serializeArray(settings, sb, path, parser, fieldName + '.' + (counter++));
} else if (token == XContentParser.Token.FIELD_NAME) {
fieldName = parser.currentName();
} else if (token == XContentParser.Token.VALUE_NULL) {
// ignore
} else {
serializeValue(settings, sb, path, jp, fieldName + '.' + (counter++));
serializeValue(settings, sb, path, parser, fieldName + '.' + (counter++));
}
}
}
private void serializeValue(Map<String, String> settings, StringBuilder sb, List<String> path, JsonParser jp, String fieldName) throws IOException {
private void serializeValue(Map<String, String> settings, StringBuilder sb, List<String> path, XContentParser parser, String fieldName) throws IOException {
sb.setLength(0);
for (String pathEle : path) {
sb.append(pathEle).append('.');
}
sb.append(fieldName);
settings.put(sb.toString(), jp.getText());
settings.put(sb.toString(), parser.text());
}
}

View File

@ -23,7 +23,6 @@ import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.ElasticSearchIllegalStateException;
import org.elasticsearch.util.xcontent.builder.BinaryXContentBuilder;
import org.elasticsearch.util.xcontent.builder.TextXContentBuilder;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import org.elasticsearch.util.xcontent.json.JsonXContent;
import java.io.IOException;
@ -42,6 +41,10 @@ public class XContentFactory {
contents[0] = new JsonXContent();
}
public static BinaryXContentBuilder jsonBuilder() throws IOException {
return contentBinaryBuilder(XContentType.JSON);
}
public static BinaryXContentBuilder contentBuilder(XContentType type) throws IOException {
if (type == XContentType.JSON) {
return JsonXContent.contentBinaryBuilder();

View File

@ -21,10 +21,11 @@ package org.elasticsearch.util.xcontent.json;
import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParser;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.util.ThreadLocals;
import org.elasticsearch.util.io.FastStringReader;
import org.elasticsearch.util.json.Jackson;
import org.elasticsearch.util.xcontent.XContent;
import org.elasticsearch.util.xcontent.XContentGenerator;
import org.elasticsearch.util.xcontent.XContentParser;
@ -101,7 +102,9 @@ public class JsonXContent implements XContent {
private final JsonFactory jsonFactory;
public JsonXContent() {
jsonFactory = Jackson.defaultJsonFactory();
jsonFactory = new JsonFactory();
jsonFactory.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
jsonFactory.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, true);
}
@Override public XContentType type() {

View File

@ -54,10 +54,6 @@ public class XContentMapConverter {
}
private static List<Object> readList(XContentParser parser, XContentParser.Token t) throws IOException {
if (t == XContentParser.Token.START_ARRAY) {
t = parser.nextToken();
}
ArrayList<Object> list = new ArrayList<Object>();
while ((t = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
list.add(readValue(parser, t));

View File

@ -19,7 +19,8 @@
package org.elasticsearch.cluster.metadata;
import org.elasticsearch.util.json.Jackson;
import org.elasticsearch.util.xcontent.XContentFactory;
import org.elasticsearch.util.xcontent.XContentType;
import org.testng.annotations.Test;
import java.io.IOException;
@ -59,10 +60,10 @@ public class ToAndFromJsonMetaDataTests {
.putMapping("mapping2", MAPPING_SOURCE2))
.build();
String metaDataSource = MetaData.Builder.toJson(metaData);
String metaDataSource = MetaData.Builder.toXContent(metaData);
System.out.println("ToJson: " + metaDataSource);
MetaData parsedMetaData = MetaData.Builder.fromJson(Jackson.defaultJsonFactory().createJsonParser(metaDataSource), null);
MetaData parsedMetaData = MetaData.Builder.fromXContent(XContentFactory.xContent(XContentType.JSON).createParser(metaDataSource), null);
assertThat(parsedMetaData.maxNumberOfShardsPerNode(), equalTo(2));
IndexMetaData indexMetaData = metaData.index("test1");

View File

@ -17,23 +17,16 @@
* under the License.
*/
package org.elasticsearch.util.json;
package org.elasticsearch.util.xcontent.builder;
import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonNode;
import org.elasticsearch.util.MapBuilder;
import org.elasticsearch.util.io.FastByteArrayInputStream;
import org.elasticsearch.util.io.FastByteArrayOutputStream;
import org.elasticsearch.util.io.FastCharArrayWriter;
import org.joda.time.DateTime;
import org.elasticsearch.util.xcontent.XContentFactory;
import org.elasticsearch.util.xcontent.XContentGenerator;
import org.elasticsearch.util.xcontent.XContentType;
import org.testng.annotations.Test;
import java.util.Date;
import java.util.Map;
import static org.elasticsearch.util.json.Jackson.*;
import static org.elasticsearch.util.json.JsonBuilder.FieldCaseConversion.*;
import static org.elasticsearch.util.xcontent.builder.XContentBuilder.FieldCaseConversion.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
@ -41,11 +34,11 @@ import static org.hamcrest.Matchers.*;
* @author kimchy (shay.banon)
*/
@Test
public class JsonBuilderTests {
public class XContentBuilderTests {
@Test public void verifyReuseJsonGenerator() throws Exception {
FastCharArrayWriter writer = new FastCharArrayWriter();
org.codehaus.jackson.JsonGenerator generator = Jackson.defaultJsonFactory().createJsonGenerator(writer);
XContentGenerator generator = XContentFactory.xContent(XContentType.JSON).createGenerator(writer);
generator.writeStartObject();
generator.writeStringField("test", "value");
generator.writeEndObject();
@ -63,8 +56,8 @@ public class JsonBuilderTests {
assertThat(writer.toStringTrim(), equalTo("{\"test\":\"value\"}"));
}
@Test public void testSimpleJacksonGenerator() throws Exception {
StringJsonBuilder builder = JsonBuilder.stringJsonBuilder();
@Test public void testSimpleGenerator() throws Exception {
TextXContentBuilder builder = XContentFactory.contentTextBuilder(XContentType.JSON);
builder.startObject().field("test", "value").endObject();
assertThat(builder.string(), equalTo("{\"test\":\"value\"}"));
builder.reset();
@ -75,7 +68,7 @@ public class JsonBuilderTests {
@Test public void testWritingBinaryToStream() throws Exception {
FastByteArrayOutputStream bos = new FastByteArrayOutputStream();
JsonGenerator gen = Jackson.defaultJsonFactory().createJsonGenerator(bos, JsonEncoding.UTF8);
XContentGenerator gen = XContentFactory.xContent(XContentType.JSON).createGenerator(bos);
gen.writeStartObject();
gen.writeStringField("name", "something");
gen.flush();
@ -87,28 +80,15 @@ public class JsonBuilderTests {
byte[] data = bos.copiedByteArray();
String sData = new String(data, "UTF8");
System.out.println("DATA: " + sData);
JsonNode node = Jackson.newObjectMapper().readValue(new FastByteArrayInputStream(data), JsonNode.class);
assertThat(node.get("source").get("test").getTextValue(), equalTo("value"));
}
@Test public void testDatesObjectMapper() throws Exception {
Date date = new Date();
DateTime dateTime = new DateTime();
Map<String, Object> data = MapBuilder.<String, Object>newMapBuilder()
.put("date", date)
.put("dateTime", dateTime)
.map();
System.out.println("Data: " + defaultObjectMapper().writeValueAsString(data));
}
@Test public void testFieldCaseConversion() throws Exception {
StringJsonBuilder builder = JsonBuilder.stringJsonBuilder().fieldCaseConversion(CAMELCASE);
TextXContentBuilder builder = XContentFactory.contentTextBuilder(XContentType.JSON).fieldCaseConversion(CAMELCASE);
builder.startObject().field("test_name", "value").endObject();
assertThat(builder.string(), equalTo("{\"testName\":\"value\"}"));
builder = JsonBuilder.stringJsonBuilder().fieldCaseConversion(UNDERSCORE);
builder = XContentFactory.contentTextBuilder(XContentType.JSON).fieldCaseConversion(UNDERSCORE);
builder.startObject().field("testName", "value").endObject();
assertThat(builder.string(), equalTo("{\"test_name\":\"value\"}"));
}
}
}

View File

@ -28,7 +28,9 @@ import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.action.support.broadcast.BroadcastOperationThreading;
import org.elasticsearch.test.integration.AbstractNodesTests;
import org.elasticsearch.util.Unicode;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.xcontent.XContentFactory;
import org.elasticsearch.util.xcontent.XContentType;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
@ -36,7 +38,6 @@ import java.io.IOException;
import static org.elasticsearch.client.Requests.*;
import static org.elasticsearch.index.query.xcontent.QueryBuilders.*;
import static org.elasticsearch.util.json.JsonBuilder.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
@ -113,7 +114,7 @@ public class BroadcastActionsTests extends AbstractNodesTests {
}
private JsonBuilder source(String id, String nameValue) throws IOException {
return jsonBuilder().startObject().field("id", id).field("name", nameValue).endObject();
private XContentBuilder source(String id, String nameValue) throws IOException {
return XContentFactory.contentBinaryBuilder(XContentType.JSON).startObject().field("id", id).field("name", nameValue).endObject();
}
}

View File

@ -29,7 +29,7 @@ import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static org.elasticsearch.client.Requests.*;
import static org.elasticsearch.util.json.JsonBuilder.*;
import static org.elasticsearch.util.xcontent.XContentFactory.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
@ -73,8 +73,8 @@ public class MoreLikeThisActionTests extends AbstractNodesTests {
assertThat(clusterHealth.status(), equalTo(ClusterHealthStatus.GREEN));
logger.info("Indexing...");
client1.index(indexRequest("test").type("type1").id("1").source(binaryJsonBuilder().startObject().field("text", "lucene").endObject())).actionGet();
client1.index(indexRequest("test").type("type1").id("2").source(binaryJsonBuilder().startObject().field("text", "lucene release").endObject())).actionGet();
client1.index(indexRequest("test").type("type1").id("1").source(jsonBuilder().startObject().field("text", "lucene").endObject())).actionGet();
client1.index(indexRequest("test").type("type1").id("2").source(jsonBuilder().startObject().field("text", "lucene release").endObject())).actionGet();
client1.admin().indices().refresh(refreshRequest()).actionGet();
logger.info("Running moreLikeThis");

View File

@ -29,14 +29,14 @@ import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.test.integration.AbstractNodesTests;
import org.elasticsearch.util.Unicode;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import java.io.IOException;
import static org.elasticsearch.client.Requests.*;
import static org.elasticsearch.util.json.JsonBuilder.*;
import static org.elasticsearch.util.xcontent.XContentFactory.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
@ -108,12 +108,12 @@ public class TransportSearchFailuresTests extends AbstractNodesTests {
client.index(Requests.indexRequest("test").type("type1").id(id).source(source(id, nameValue, age))).actionGet();
}
private JsonBuilder source(String id, String nameValue, int age) throws IOException {
private XContentBuilder source(String id, String nameValue, int age) throws IOException {
StringBuilder multi = new StringBuilder().append(nameValue);
for (int i = 0; i < age; i++) {
multi.append(" ").append(nameValue);
}
return binaryJsonBuilder().startObject()
return jsonBuilder().startObject()
.field("id", id)
.field("name", nameValue + id)
.field("age", age)

View File

@ -19,7 +19,6 @@
package org.elasticsearch.test.integration.search;
import org.elasticsearch.util.gcommon.collect.Sets;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.action.search.SearchPhaseExecutionException;
import org.elasticsearch.action.search.SearchResponse;
@ -30,7 +29,8 @@ import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.test.integration.AbstractNodesTests;
import org.elasticsearch.util.Unicode;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.gcommon.collect.Sets;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@ -44,7 +44,7 @@ import static org.elasticsearch.client.Requests.*;
import static org.elasticsearch.index.query.xcontent.QueryBuilders.*;
import static org.elasticsearch.search.builder.SearchSourceBuilder.*;
import static org.elasticsearch.util.TimeValue.*;
import static org.elasticsearch.util.json.JsonBuilder.*;
import static org.elasticsearch.util.xcontent.XContentFactory.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
@ -313,12 +313,12 @@ public class TransportTwoServersSearchTests extends AbstractNodesTests {
client.index(Requests.indexRequest("test").type("type1").id(id).source(source(id, nameValue, age))).actionGet();
}
private JsonBuilder source(String id, String nameValue, int age) throws IOException {
private XContentBuilder source(String id, String nameValue, int age) throws IOException {
StringBuilder multi = new StringBuilder().append(nameValue);
for (int i = 0; i < age; i++) {
multi.append(" ").append(nameValue);
}
return binaryJsonBuilder().startObject()
return jsonBuilder().startObject()
.field("id", id)
.field("name", nameValue + id)
.field("age", age)

View File

@ -25,7 +25,8 @@ import org.elasticsearch.client.Requests;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.test.integration.AbstractNodesTests;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.xcontent.XContentFactory;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@ -38,7 +39,7 @@ import static org.elasticsearch.client.Requests.*;
import static org.elasticsearch.index.query.xcontent.QueryBuilders.*;
import static org.elasticsearch.search.builder.SearchSourceBuilder.*;
import static org.elasticsearch.util.TimeValue.*;
import static org.elasticsearch.util.json.JsonBuilder.*;
import static org.elasticsearch.util.xcontent.XContentFactory.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
@ -147,18 +148,18 @@ public class HighlightSearchTests extends AbstractNodesTests {
client.index(Requests.indexRequest("test").type("type1").id(id).source(source(id, nameValue, age))).actionGet();
}
public JsonBuilder mapping() throws IOException {
return binaryJsonBuilder().startObject().startObject("type1")
public XContentBuilder mapping() throws IOException {
return XContentFactory.jsonBuilder().startObject().startObject("type1")
.startObject("_all").field("store", "yes").field("termVector", "with_positions_offsets").endObject()
.endObject().endObject();
}
private JsonBuilder source(String id, String nameValue, int age) throws IOException {
private XContentBuilder source(String id, String nameValue, int age) throws IOException {
StringBuilder multi = new StringBuilder().append(nameValue);
for (int i = 0; i < age; i++) {
multi.append(" ").append(nameValue);
}
return binaryJsonBuilder().startObject()
return jsonBuilder().startObject()
.field("id", id)
.field("name", nameValue + id)
.field("age", age)

View File

@ -30,7 +30,7 @@ import org.testng.annotations.Test;
import static org.elasticsearch.client.Requests.*;
import static org.elasticsearch.index.query.xcontent.QueryBuilders.*;
import static org.elasticsearch.search.builder.SearchSourceBuilder.*;
import static org.elasticsearch.util.json.JsonBuilder.*;
import static org.elasticsearch.util.xcontent.XContentFactory.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

View File

@ -34,7 +34,7 @@ import org.testng.annotations.Test;
import static org.elasticsearch.action.terms.TermsRequest.SortType.*;
import static org.elasticsearch.client.Requests.*;
import static org.elasticsearch.util.MapBuilder.*;
import static org.elasticsearch.util.json.JsonBuilder.*;
import static org.elasticsearch.util.xcontent.XContentFactory.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
@ -81,7 +81,7 @@ public class TermsActionTests extends AbstractNodesTests {
assertThat("no term freqs for the 'value' since nothing is indexed", termsResponse.field("value").iterator().hasNext(), equalTo(false));
logger.info("Index [1]");
client.index(indexRequest("test").type("type1").id("1").source(binaryJsonBuilder().startObject().field("value", "aaa").endObject())).actionGet();
client.index(indexRequest("test").type("type1").id("1").source(jsonBuilder().startObject().field("value", "aaa").endObject())).actionGet();
logger.info("Refresh");
client.admin().indices().refresh(refreshRequest()).actionGet();
@ -97,7 +97,7 @@ public class TermsActionTests extends AbstractNodesTests {
assertThat(termsResponse.field("value").docFreq("bbb"), equalTo(-1));
logger.info("Index [2]");
client.index(indexRequest("test").type("type1").id("2").source(binaryJsonBuilder().startObject().field("value", "bbb bbb").endObject())).actionGet();
client.index(indexRequest("test").type("type1").id("2").source(jsonBuilder().startObject().field("value", "bbb bbb").endObject())).actionGet();
logger.info("Refresh");
client.admin().indices().refresh(refreshRequest()).actionGet();
@ -124,7 +124,7 @@ public class TermsActionTests extends AbstractNodesTests {
assertThat(termsResponse.field("_all").docFreq("bbb"), equalTo(1));
logger.info("Delete 3");
client.index(indexRequest("test").type("type1").id("3").source(binaryJsonBuilder().startObject().field("value", "bbb").endObject())).actionGet();
client.index(indexRequest("test").type("type1").id("3").source(jsonBuilder().startObject().field("value", "bbb").endObject())).actionGet();
logger.info("Refresh");
client.admin().indices().refresh(refreshRequest()).actionGet();

View File

@ -19,10 +19,10 @@
package org.elasticsearch.groovy.util.json
import org.elasticsearch.ElasticSearchGenerationException
import org.elasticsearch.util.io.FastByteArrayOutputStream
import org.elasticsearch.util.io.FastCharArrayWriter
import static org.elasticsearch.util.json.Jackson.*
import org.elasticsearch.util.xcontent.XContentFactory
import org.elasticsearch.util.xcontent.XContentType
import org.elasticsearch.util.xcontent.builder.BinaryXContentBuilder
import org.elasticsearch.util.xcontent.builder.TextXContentBuilder
/**
* Used to build JSON data.
@ -49,25 +49,17 @@ class JsonBuilder {
}
String buildAsString(Closure c) {
FastCharArrayWriter writer = FastCharArrayWriter.Cached.cached();
try {
def json = build(c)
defaultObjectMapper().writeValue(writer, json);
} catch (IOException e) {
throw new ElasticSearchGenerationException("Failed to generate [" + c + "]", e);
}
return writer.toStringTrim()
TextXContentBuilder builder = XContentFactory.contentTextBuilder(XContentType.JSON);
def json = build(c)
builder.map(json);
return builder.string();
}
byte[] buildAsBytes(Closure c) {
FastByteArrayOutputStream os = FastByteArrayOutputStream.Cached.cached();
try {
def json = build(c)
defaultObjectMapper().writeValue(os, json);
} catch (IOException e) {
throw new ElasticSearchGenerationException("Failed to generate [" + c + "]", e);
}
return os.copiedByteArray()
BinaryXContentBuilder builder = XContentFactory.contentBinaryBuilder(XContentType.JSON);
def json = build(c)
builder.map(json);
return builder.copiedBytes();
}
private buildRoot(Closure c) {

View File

@ -26,7 +26,7 @@ import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import static org.elasticsearch.util.io.Streams.*;
import static org.elasticsearch.util.json.JsonBuilder.*;
import static org.elasticsearch.util.xcontent.XContentFactory.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

View File

@ -31,8 +31,8 @@ import static org.elasticsearch.client.Requests.*;
import static org.elasticsearch.index.query.xcontent.QueryBuilders.*;
import static org.elasticsearch.node.NodeBuilder.*;
import static org.elasticsearch.util.io.Streams.*;
import static org.elasticsearch.util.json.JsonBuilder.*;
import static org.elasticsearch.util.settings.ImmutableSettings.*;
import static org.elasticsearch.util.xcontent.XContentFactory.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

View File

@ -30,7 +30,7 @@ import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import static org.elasticsearch.node.NodeBuilder.*;
import static org.elasticsearch.util.json.JsonBuilder.*;
import static org.elasticsearch.util.xcontent.XContentFactory.*;
/**
* @author kimchy (shay.banon)
@ -56,7 +56,7 @@ public abstract class AbstractMemcachedActionsTests {
}
@Test public void testSimpleOperations() throws Exception {
Future setResult = memcachedClient.set("/test/person/1", 0, binaryJsonBuilder().startObject().field("test", "value").endObject().copiedBytes());
Future setResult = memcachedClient.set("/test/person/1", 0, jsonBuilder().startObject().field("test", "value").endObject().copiedBytes());
setResult.get(10, TimeUnit.SECONDS);
String getResult = (String) memcachedClient.get("/_refresh");