下面列出了org.apache.hadoop.fs.swift.exceptions.SwiftJsonMarshallingException#org.codehaus.jackson.JsonGenerationException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@GET
@Path("{id}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response findById(@PathParam("id") Long id) throws JsonGenerationException, JsonMappingException, IOException {
Podcast podcastById = podcastDao.getPodcastById(id);
if (podcastById != null) {
return Response
.status(200)
.entity(podcastById)
.header("Access-Control-Allow-Headers", "X-extra-header")
.allow("OPTIONS")
.build();
} else {
return Response
.status(404)
.entity("The podcast with the id " + id + " does not exist")
.build();
}
}
private void addVertexToPolygon(double lat, double lon, double hae)
throws JsonGenerationException, IOException {
try {
if (firstVertex) {
firstVertex = false;
cachedLat = lat;
cachedLon = lon;
cachedHae = hae;
firstVertex = false;
}
generator.writeStartArray();
generator.writeNumber(lon);
generator.writeNumber(lat);
// generator.writeNumber(hae);
generator.writeEndArray();
} catch (Exception e) {
log.error(e);
log.error(e.getStackTrace());
}
}
private void addVertexToPolygon(double lat, double lon, double hae) throws JsonGenerationException, IOException
{
if( firstVertex )
{
firstVertex = false;
cachedLat = lat;
cachedLon = lon;
cachedHae = hae;
firstVertex = false;
}
generator.writeStartArray();
generator.writeNumber(lon);
generator.writeNumber(lat);
//generator.writeNumber(hae);
generator.writeEndArray();
}
private void addVertexToPolygon(double lat, double lon, double hae)
throws JsonGenerationException, IOException {
try {
if (firstVertex) {
firstVertex = false;
cachedLat = lat;
cachedLon = lon;
cachedHae = hae;
firstVertex = false;
}
generator.writeStartArray();
generator.writeNumber(lon);
generator.writeNumber(lat);
// generator.writeNumber(hae);
generator.writeEndArray();
} catch (Exception e) {
log.error(e);
log.error(e.getStackTrace());
}
}
private void addVertexToPolygon(double lat, double lon, double hae) throws JsonGenerationException, IOException
{
if( firstVertex )
{
firstVertex = false;
cachedLat = lat;
cachedLon = lon;
cachedHae = hae;
firstVertex = false;
}
generator.writeStartArray();
generator.writeNumber(lon);
generator.writeNumber(lat);
//generator.writeNumber(hae);
generator.writeEndArray();
}
@Test
public void testGetLegacyPodcast() throws JsonGenerationException,
JsonMappingException, IOException {
ClientConfig clientConfig = new ClientConfig();
clientConfig.register(JacksonFeature.class);
Client client = ClientBuilder.newClient(clientConfig);
WebTarget webTarget = client
.target("http://localhost:8888/demo-rest-spring-jersey-jpa2-hibernate-0.0.1-SNAPSHOT/podcasts/2");
Builder request = webTarget.request(MediaType.APPLICATION_JSON);
Response response = request.get();
Assert.assertTrue(response.getStatus() == 200);
Podcast podcast = response.readEntity(Podcast.class);
ObjectMapper mapper = new ObjectMapper();
System.out
.print("Received podcast from database *************************** "
+ mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(podcast));
}
public HashMap<String, Object> execute(String operation, String content)
throws JsonGenerationException, JsonMappingException, IOException, UserSysException
{
HashMap<String, Object> res = callOpenAPI(operation, content);
//try {
//int status = (Integer)res.get("status");
String body = (String)res.get("body");
ObjectMapper mapper = new ObjectMapper();
Object json = mapper.readValue(body, Object.class);
res.put("json", json);
//} catch (JsonGenerationException ex) {
// res.put("exception", ex);
//} catch (JsonMappingException ex) {
// res.put("exception", ex);
//} catch (IOException ex) {
// res.put("exception", ex);
//} catch (UserSysException ex) {
//}
return res;
}
StringRepresentation getInstanceRepresentation() throws JsonGenerationException,
JsonMappingException, IOException {
String clusterName =
ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.CLUSTER_NAME);
String instanceName =
ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.INSTANCE_NAME);
Builder keyBuilder = new PropertyKey.Builder(clusterName);
ZkClient zkclient =
ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.RAW_ZKCLIENT);
String instanceCfgStr =
ResourceUtil.readZkAsBytes(zkclient, keyBuilder.instanceConfig(instanceName));
StringRepresentation representation =
new StringRepresentation(instanceCfgStr, MediaType.APPLICATION_JSON);
return representation;
}
void sendMessage(String msgId, String instanceName, String fromState, String toState,
String partitionKey, int partitionId) throws InterruptedException, JsonGenerationException,
JsonMappingException, IOException {
Message message = new Message(MessageType.STATE_TRANSITION, msgId);
message.setMsgId(msgId);
message.setSrcName(srcName);
message.setTgtName(instanceName);
message.setMsgState(MessageState.NEW);
message.setFromState(fromState);
message.setToState(toState);
// message.setPartitionId(partitionId);
message.setPartitionName(partitionKey);
String path = PropertyPathBuilder.instanceMessage(clusterName, instanceName, message.getId());
ObjectMapper mapper = new ObjectMapper();
StringWriter sw = new StringWriter();
mapper.writeValueUsingView(sw, message, Message.class);
System.out.println(sw.toString());
client.delete(path);
Thread.sleep(10000);
ZNRecord record = client.readData(PropertyPathBuilder.liveInstance(clusterName, instanceName));
message.setTgtSessionId(record.getSimpleField(LiveInstanceProperty.SESSION_ID.toString())
.toString());
client.createPersistent(path, message);
}
public static String toJsonString(Object obj, boolean bStripJsReservedWords) throws JsonGenerationException, JsonMappingException, IOException {
String strResult = null;
if (obj != null) {
if (bStripJsReservedWords && obj instanceof JetstreamEvent)
obj = ((JetstreamEvent)obj).getFilteredEvent();
try {
ObjectMapper mapper = new ObjectMapper();
Writer writer = new StringWriter();
mapper.writeValue(writer, obj);
strResult = writer.toString();
}
catch (Throwable t) {
if (m_nErrorCount++ % 10000 == 0 && LOGGER.isErrorEnabled())
LOGGER.error( "", t);
}
}
return strResult;
}
StringRepresentation getInstanceStatusUpdateRepresentation(String clusterName,
String instanceName, String resourceGroup) throws JsonGenerationException,
JsonMappingException, IOException {
ZkClient zkClient = (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
String instanceSessionId =
ClusterRepresentationUtil.getInstanceSessionId(zkClient, clusterName, instanceName);
Builder keyBuilder = new PropertyKey.Builder(clusterName);
String message =
ClusterRepresentationUtil.getInstancePropertiesAsString(zkClient, clusterName,
keyBuilder.stateTransitionStatus(instanceName, instanceSessionId, resourceGroup),
// instanceSessionId
// + "__"
// + resourceGroup,
MediaType.APPLICATION_JSON);
StringRepresentation representation =
new StringRepresentation(message, MediaType.APPLICATION_JSON);
return representation;
}
private static void sendMessage() throws IOException, JsonProcessingException, JsonGenerationException,
JsonMappingException, UnsupportedEncodingException, HttpException {
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> m = new HashMap<String, Object>();
m.put("si", "12345");
m.put("ct", System.currentTimeMillis());
String payload = mapper.writeValueAsString(m);
HttpClient client = new HttpClient();
PostMethod method = new PostMethod("http://localhost:8080/tracking/ingest/PulsarRawEvent");
// method.addRequestHeader("Accept-Encoding", "gzip,deflate,sdch");
method.setRequestEntity(new StringRequestEntity(payload, "application/json", "UTF-8"));
int status = client.executeMethod(method);
System.out.println(Arrays.toString(method.getResponseHeaders()));
System.out.println("Status code: " + status + ", Body: " + method.getResponseBodyAsString());
}
public HashMap<String, Object> execute(String operation, String content)
throws JsonGenerationException, JsonMappingException, IOException, UserSysException
{
HashMap<String, Object> res = callOpenAPI(operation, content);
//try {
//int status = (Integer)res.get("status");
String body = (String)res.get("body");
ObjectMapper mapper = new ObjectMapper();
Object json = mapper.readValue(body, Object.class);
res.put("json", json);
//} catch (JsonGenerationException ex) {
// res.put("exception", ex);
//} catch (JsonMappingException ex) {
// res.put("exception", ex);
//} catch (IOException ex) {
// res.put("exception", ex);
//} catch (UserSysException ex) {
//}
return res;
}
StringRepresentation getInstanceErrorsRepresentation(String clusterName, String instanceName)
throws JsonGenerationException, JsonMappingException, IOException {
ZkClient zkClient = (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
;
String instanceSessionId =
ClusterRepresentationUtil.getInstanceSessionId(zkClient, clusterName, instanceName);
String message =
ClusterRepresentationUtil
.getInstancePropertyNameListAsString(zkClient, clusterName, instanceName,
PropertyType.CURRENTSTATES, instanceSessionId, MediaType.APPLICATION_JSON);
StringRepresentation representation =
new StringRepresentation(message, MediaType.APPLICATION_JSON);
return representation;
}
StringRepresentation getStateModelsRepresentation() throws JsonGenerationException,
JsonMappingException, IOException {
String clusterName = (String) getRequest().getAttributes().get("clusterName");
ZkClient zkClient = (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
ClusterSetup setupTool = new ClusterSetup(zkClient);
List<String> models = setupTool.getClusterManagementTool().getStateModelDefs(clusterName);
ZNRecord modelDefinitions = new ZNRecord("modelDefinitions");
modelDefinitions.setListField("models", models);
StringRepresentation representation =
new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(modelDefinitions),
MediaType.APPLICATION_JSON);
return representation;
}
@Test
void testThreeDimsTeam() throws JsonGenerationException,
JsonMappingException,
IOException,
InterruptedException
{
Object[][] rows =
{ { 1, (int) 1, (int) 2, (int) 1 }, { 2, (int) 1, (int) 2, (int) 2 },
{ 3, (int) 1, (int) 1, (int) 1 },
{ 4, (int) 1, (int) 1, (int) 2 },
{ 5, (int) 2, (int) 2, (int) 2 } };
String[] expected =
new String[] { "(1,2,1,1)", "(1,2,2,1)", "(1,1,1,1)", "(1,1,2,1)",
"(2,2,2,1)", "(1,,,4)", "(2,,,1)", "(,1,,2)", "(,2,,3)",
"(,,1,2)", "(,,2,3)", "(1,1,,2)", "(1,2,,2)", "(2,2,,1)",
"(,1,1,1)", "(,1,2,1)", "(,2,1,1)", "(,2,2,2)", "(1,,1,2)",
"(1,,2,2)", "(2,,2,1)", "(,,,5)" };
validate(rows, expected);
}
@Test
void testLongType() throws JsonGenerationException,
JsonMappingException,
IOException,
InterruptedException
{
// members: srinivas, maneesh, krishna, saurabh, rui
// dimensions: country code, number of monitors, vegetarian
Object[][] rows =
{ { 1, (long) 1, (long) 2, (long) 1 },
{ 2, (long) 1, (long) 2, (long) 2 },
{ 3, (long) 1, (long) 1, (long) 1 },
{ 4, (long) 1, (long) 1, (long) 2 },
{ 5, (long) 2, (long) 2, (long) 2 } };
String[] expected =
new String[] { "(1,2,1,1)", "(1,2,2,1)", "(1,1,1,1)", "(1,1,2,1)",
"(2,2,2,1)", "(1,,,4)", "(2,,,1)", "(,1,,2)", "(,2,,3)",
"(,,1,2)", "(,,2,3)", "(1,1,,2)", "(1,2,,2)", "(2,2,,1)",
"(,1,1,1)", "(,1,2,1)", "(,2,1,1)", "(,2,2,2)", "(1,,1,2)",
"(1,,2,2)", "(2,,2,1)", "(,,,5)" };
validate(rows, expected);
}
@Test
void testMixedTypes() throws JsonGenerationException,
JsonMappingException,
IOException,
InterruptedException
{
// members: srinivas, maneesh, krishna, saurabh, rui
// dimensions: country code, number of monitors, vegetarian
Object[][] rows =
{ { 1, 1, (long) 22222222222222L, 1 },
{ 2, 1, (long) 22222222222222L, 2 },
{ 3, 1, (long) 11111111111111L, 1 },
{ 4, 1, (long) 11111111111111L, 2 },
{ 5, 2, (long) 22222222222222L, 2 } };
String[] expected =
new String[] { "(1,22222222222222,1,1)", "(1,22222222222222,2,1)",
"(1,11111111111111,1,1)", "(1,11111111111111,2,1)",
"(2,22222222222222,2,1)", "(1,,,4)", "(2,,,1)",
"(,11111111111111,,2)", "(,22222222222222,,3)", "(,,1,2)",
"(,,2,3)", "(1,11111111111111,,2)", "(1,22222222222222,,2)",
"(2,22222222222222,,1)", "(,11111111111111,1,1)",
"(,11111111111111,2,1)", "(,22222222222222,1,1)",
"(,22222222222222,2,2)", "(1,,1,2)", "(1,,2,2)", "(2,,2,1)",
"(,,,5)" };
validate(rows, expected);
}
@Test
void testThreeDimsTeamGroupingSets() throws JsonGenerationException,
JsonMappingException,
IOException,
InterruptedException
{
// members: srinivas, maneesh, krishna, saurabh, rui
// dimensions: country code, number of monitors, vegetarian
Object[][] rows =
{ { 1, (int) 1, (int) 2, (int) 1 }, { 2, (int) 1, (int) 2, (int) 2 },
{ 3, (int) 1, (int) 1, (int) 1 },
{ 4, (int) 1, (int) 1, (int) 2 },
{ 5, (int) 2, (int) 2, (int) 2 } };
String[] expected =
new String[] { "(1,,,4)", "(2,,,1)", "(1,1,,2)", "(1,2,,2)", "(2,2,,1)" };
validateGroupingSets(rows, expected, new String[] { "Dim0,Dim1", "Dim0" });
}
StringRepresentation getInstanceCurrentStateRepresentation(String clusterName,
String instanceName, String resourceGroup) throws JsonGenerationException,
JsonMappingException, IOException {
ZkClient zkClient = (ZkClient) getRequest().getAttributes().get(RestAdminApplication.ZKCLIENT);
String instanceSessionId =
ClusterRepresentationUtil.getInstanceSessionId(zkClient, clusterName, instanceName);
Builder keyBuilder = new PropertyKey.Builder(clusterName);
String message =
ClusterRepresentationUtil.getInstancePropertyAsString(zkClient, clusterName,
keyBuilder.currentState(instanceName, instanceSessionId, resourceGroup),
MediaType.APPLICATION_JSON);
StringRepresentation representation =
new StringRepresentation(message, MediaType.APPLICATION_JSON);
return representation;
}
@Test
void testGroupingSetsSum() throws JsonGenerationException,
JsonMappingException,
IOException,
InterruptedException
{
// clickCount
// dimensions: country code, number of monitors, vegetarian
Object[][] rows =
{ { 1, (int) 1, (int) 1, (int) 1 }, { 1, (int) 1, (int) 1, (int) 2 },
{ 2, (int) 1, (int) 2, (int) 1 },
{ 3, (int) 1, (int) 2, (int) 2 },
{ 2, (int) 2, (int) 2, (int) 2 } };
String[] expected =
new String[] { "(1,,,7)", "(2,,,2)", "(1,1,,2)", "(1,2,,5)", "(2,2,,2)" };
validateGroupingSets(rows, expected, new String[] { "Dim0,Dim1", "Dim0" });
}
StringRepresentation getSchedulerTasksRepresentation() throws JsonGenerationException,
JsonMappingException, IOException {
String clusterName = (String) getRequest().getAttributes().get("clusterName");
String instanceName = (String) getRequest().getAttributes().get("instanceName");
ZkClient zkClient = (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
ClusterSetup setupTool = new ClusterSetup(zkClient);
List<String> instances =
setupTool.getClusterManagementTool().getInstancesInCluster(clusterName);
HelixDataAccessor accessor =
ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
LiveInstance liveInstance =
accessor.getProperty(accessor.keyBuilder().liveInstance(instanceName));
String sessionId = liveInstance.getEphemeralOwner();
StringRepresentation representation = new StringRepresentation("");// (ClusterRepresentationUtil.ObjectToJson(instanceConfigs),
// MediaType.APPLICATION_JSON);
return representation;
}
/**
* TODO If we could get the nasty pre-serialization stuff into the serialization policy, that'd be great.
*/
public static String getRequestString(final String method, final Object... parameters) throws JsonGenerationException, JsonMappingException, IOException {
final ArrayList<Object> parameterLst = new ArrayList<>();
for (Object parameter : parameters) {
// Just check if this parameter is a number, if it is, make it an explicit
// number
if (parameter instanceof String) {
if (((String) parameter).toLowerCase().equals("true")) {
parameter = true;
} else if (((String) parameter).toLowerCase().equals("false")) {
parameter = false;
} else if (parameter instanceof String) {
try {
parameter = Integer.parseInt((String) parameter);
} catch (final NumberFormatException e) {
// eat
}
}
}
parameterLst.add(parameter);
}
return getRequestStringWithParameters(method, parameterLst);
}
@Test
public void testGetLegacyPodcast() throws JsonGenerationException,
JsonMappingException, IOException {
ClientConfig clientConfig = new ClientConfig();
clientConfig.register(JacksonFeature.class);
Client client = ClientBuilder.newClient(clientConfig);
WebTarget webTarget = client
.target("http://localhost:8888/demo-rest-jersey-spring/legacy/podcasts/2");
Builder request = webTarget.request(MediaType.APPLICATION_JSON);
Response response = request.get();
Assert.assertTrue(response.getStatus() == 200);
Podcast podcast = response.readEntity(Podcast.class);
ObjectMapper mapper = new ObjectMapper();
System.out
.print("Received podcast from LEGACY database *************************** "
+ mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(podcast));
}
/**
* Convenience method to serialize a JSON object that also handles the simple string case.
*/
public static String asString(Object jsonObj) throws JsonGenerationException, JsonMappingException, IOException {
if (jsonObj == null) {
return "";
}
if (jsonObj instanceof String) {
return (String) jsonObj;
} else {
return (new ObjectMapper()).writeValueAsString(jsonObj);
}
}
public File writeJsonToFile(ViewBaseBean viewBean, String fileName)
throws JsonGenerationException, JsonMappingException, IOException {
if (fileName.length() < 3) {
fileName = "file_" + fileName;
}
File file = File.createTempFile(fileName, ".json");
JsonUtilsV2.getMapper().defaultPrettyPrintingWriter().writeValue(file, viewBean);
return file;
}
StringRepresentation getClusterRepresentation(String clusterName) throws JsonGenerationException,
JsonMappingException, IOException {
ZkClient zkClient =
ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
ClusterSetup setupTool = new ClusterSetup(zkClient);
List<String> instances =
setupTool.getClusterManagementTool().getInstancesInCluster(clusterName);
ZNRecord clusterSummayRecord = new ZNRecord("Cluster Summary");
clusterSummayRecord.setListField("participants", instances);
List<String> resources =
setupTool.getClusterManagementTool().getResourcesInCluster(clusterName);
clusterSummayRecord.setListField("resources", resources);
List<String> models = setupTool.getClusterManagementTool().getStateModelDefs(clusterName);
clusterSummayRecord.setListField("stateModelDefs", models);
HelixDataAccessor accessor =
ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
Builder keyBuilder = accessor.keyBuilder();
LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
if (leader != null) {
clusterSummayRecord.setSimpleField("LEADER", leader.getInstanceName());
} else {
clusterSummayRecord.setSimpleField("LEADER", "");
}
StringRepresentation representation =
new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(clusterSummayRecord),
MediaType.APPLICATION_JSON);
return representation;
}
public byte[] writeValueAsBytes(Object value,
JsonSerialize.Inclusion inc) throws IOException,
JsonGenerationException, JsonMappingException {
if (inc == null) {
return super.writeValueAsBytes(value);
}
// alas, we have to pull the recycler directly here...
ByteArrayBuilder bb = new ByteArrayBuilder(_jsonFactory._getBufferRecycler());
writeValueWithConf(_jsonFactory.createJsonGenerator(bb, JsonEncoding.UTF8), value,inc);
byte[] result = bb.toByteArray();
bb.release();
return result;
}
private void writeValueWithConf(JsonGenerator jgen, Object value,
JsonSerialize.Inclusion inc) throws IOException,
JsonGenerationException, JsonMappingException {
SerializationConfig cfg = copySerializationConfig();
cfg = cfg.withSerializationInclusion(inc);
// [JACKSON-96]: allow enabling pretty printing for ObjectMapper
// directly
if (cfg.isEnabled(SerializationConfig.Feature.INDENT_OUTPUT)) {
jgen.useDefaultPrettyPrinter();
}
// [JACKSON-282]: consider Closeable
if (cfg.isEnabled(SerializationConfig.Feature.CLOSE_CLOSEABLE)
&& (value instanceof Closeable)) {
configAndWriteCloseable(jgen, value, cfg);
return;
}
boolean closed = false;
try {
_serializerProvider.serializeValue(cfg, jgen, value,
_serializerFactory);
closed = true;
jgen.close();
} finally {
/*
* won't try to close twice; also, must catch exception (so it
* will not mask exception that is pending)
*/
if (!closed) {
try {
jgen.close();
} catch (IOException ioe) {
}
}
}
}
public static String getInstancePropertyNameListAsString(ZkClient zkClient, String clusterName,
String instanceName, PropertyType instanceProperty, String key, MediaType mediaType)
throws JsonGenerationException, JsonMappingException, IOException {
String path = PropertyPathBuilder.instanceProperty(clusterName, instanceName, instanceProperty, key);
if (zkClient.exists(path)) {
List<String> recordNames = zkClient.getChildren(path);
return ObjectToJson(recordNames);
}
return ObjectToJson(new ArrayList<String>());
}