Created
February 10, 2011 21:06
-
-
Save pierre/821341 to your computer and use it in GitHub Desktop.
auto detection rullez
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2011 Ning, Inc. | |
* | |
* Ning 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 com.ning.metrics.serialization.smile; | |
import org.codehaus.jackson.JsonFactory; | |
import org.codehaus.jackson.JsonNode; | |
import org.codehaus.jackson.JsonParser; | |
import org.codehaus.jackson.map.ObjectMapper; | |
import org.codehaus.jackson.smile.SmileFactory; | |
import org.codehaus.jackson.smile.SmileGenerator; | |
import org.codehaus.jackson.smile.SmileParser; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.PushbackInputStream; | |
public class SmileBucketDeserializer | |
{ | |
protected final static SmileFactory smileFactory = new SmileFactory(); | |
protected final static JsonFactory jsonFactory = new JsonFactory(); | |
static { | |
// yes, full 'compression' by checking for repeating names, short string values: | |
smileFactory.configure(SmileGenerator.Feature.CHECK_SHARED_NAMES, true); | |
smileFactory.configure(SmileGenerator.Feature.CHECK_SHARED_STRING_VALUES, true); | |
// and for now let's not mandate header for input | |
smileFactory.configure(SmileParser.Feature.REQUIRE_HEADER, false); | |
} | |
private static final ObjectMapper smileObjectMapper = new ObjectMapper(smileFactory); | |
private static final ObjectMapper jsonObjectMapper = new ObjectMapper(jsonFactory); | |
private static final byte SMILE_MARKER = ':'; | |
public static SmileBucket deserialize(InputStream in) throws IOException | |
{ | |
PushbackInputStream pbIn = new PushbackInputStream(in); | |
byte firstByte = (byte) pbIn.read(); | |
pbIn.unread(firstByte); | |
if (firstByte == SMILE_MARKER) { | |
return deserialize(pbIn, smileObjectMapper); | |
} | |
else { | |
return deserialize(pbIn, jsonObjectMapper); | |
} | |
} | |
public static SmileBucket deserialize(InputStream in, ObjectMapper objectMapper) throws IOException | |
{ | |
SmileBucket bucket = new SmileBucket(); | |
JsonParser jp = objectMapper.getJsonFactory().createJsonParser(in); | |
while (jp.nextToken() != null) { | |
bucket.add(objectMapper.readValue(jp, JsonNode.class)); | |
} | |
jp.close(); | |
return bucket; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment