Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON abstraction for JSON RPC support #390

Merged
merged 4 commits into from
Aug 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<slf4j.version>1.7.25</slf4j.version>
<metrics.version>3.2.6</metrics.version>
<micrometer.version>1.0.2</micrometer.version>
<jackson.version>2.9.6</jackson.version>
<logback.version>1.2.3</logback.version>
<commons-cli.version>1.1</commons-cli.version>
<junit.version>4.12</junit.version>
Expand Down Expand Up @@ -640,6 +641,12 @@
<version>${micrometer.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/rabbitmq/tools/json/JSONReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
import java.util.List;
import java.util.Map;

/**
* Will be removed in 6.0
*
* @deprecated Use a third-party JSON library, e.g. Jackson or Gson
*/
public class JSONReader {

private static final Object OBJECT_END = new Object();
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/rabbitmq/tools/json/JSONSerializable.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

/**
* Interface for classes that wish to control their own serialization.
*
* Will be removed in 6.0
*
* @deprecated Use a third-party JSON library, e.g. Jackson or Gson
*/
public interface JSONSerializable {
/**
Expand Down
81 changes: 38 additions & 43 deletions src/main/java/com/rabbitmq/tools/json/JSONUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// If you have any questions regarding licensing, please contact us at
// info@rabbitmq.com.


package com.rabbitmq.tools.json;

import org.slf4j.Logger;
Expand All @@ -34,56 +33,52 @@
*/
public class JSONUtil {

private static final Logger LOGGER = LoggerFactory.getLogger(JSONUtil.class);
private static final Logger LOGGER = LoggerFactory.getLogger(JSONUtil.class);

/**
* Uses reflection to fill public fields and Bean properties of
* the target object from the source Map.
*/
public static Object fill(Object target, Map<String, Object> source)
throws IntrospectionException, IllegalAccessException, InvocationTargetException
{
return fill(target, source, true);
throws IntrospectionException, IllegalAccessException, InvocationTargetException {
return fill(target, source, true);
}

/**
* Uses reflection to fill public fields and optionally Bean
* properties of the target object from the source Map.
*/
public static Object fill(Object target, Map<String, Object> source, boolean useProperties)
throws IntrospectionException, IllegalAccessException, InvocationTargetException
{
if (useProperties) {
BeanInfo info = Introspector.getBeanInfo(target.getClass());
throws IntrospectionException, IllegalAccessException, InvocationTargetException {
if (useProperties) {
BeanInfo info = Introspector.getBeanInfo(target.getClass());

PropertyDescriptor[] props = info.getPropertyDescriptors();
for (int i = 0; i < props.length; ++i) {
PropertyDescriptor prop = props[i];
String name = prop.getName();
Method setter = prop.getWriteMethod();
if (setter != null && !Modifier.isStatic(setter.getModifiers())) {
//System.out.println(target + " " + name + " <- " + source.get(name));
setter.invoke(target, source.get(name));
}
}
}
PropertyDescriptor[] props = info.getPropertyDescriptors();
for (int i = 0; i < props.length; ++i) {
PropertyDescriptor prop = props[i];
String name = prop.getName();
Method setter = prop.getWriteMethod();
if (setter != null && !Modifier.isStatic(setter.getModifiers())) {
setter.invoke(target, source.get(name));
}
}
}

Field[] ff = target.getClass().getDeclaredFields();
for (int i = 0; i < ff.length; ++i) {
Field field = ff[i];
Field[] ff = target.getClass().getDeclaredFields();
for (int i = 0; i < ff.length; ++i) {
Field field = ff[i];
int fieldMod = field.getModifiers();
if (Modifier.isPublic(fieldMod) && !(Modifier.isFinal(fieldMod) ||
Modifier.isStatic(fieldMod)))
{
//System.out.println(target + " " + field.getName() + " := " + source.get(field.getName()));
try {
field.set(target, source.get(field.getName()));
} catch (IllegalArgumentException iae) {
// no special error processing required
if (Modifier.isPublic(fieldMod) && !(Modifier.isFinal(fieldMod) ||
Modifier.isStatic(fieldMod))) {
try {
field.set(target, source.get(field.getName()));
} catch (IllegalArgumentException iae) {
// no special error processing required
}
}
}
}
}

return target;
return target;
}

/**
Expand All @@ -92,14 +87,14 @@ public static Object fill(Object target, Map<String, Object> source, boolean use
* source Map.
*/
public static void tryFill(Object target, Map<String, Object> source) {
try {
fill(target, source);
} catch (IntrospectionException ie) {
LOGGER.error("Error in tryFill", ie);
} catch (IllegalAccessException iae) {
LOGGER.error("Error in tryFill", iae);
} catch (InvocationTargetException ite) {
LOGGER.error("Error in tryFill", ite);
}
try {
fill(target, source);
} catch (IntrospectionException ie) {
LOGGER.error("Error in tryFill", ie);
} catch (IllegalAccessException iae) {
LOGGER.error("Error in tryFill", iae);
} catch (InvocationTargetException ite) {
LOGGER.error("Error in tryFill", ite);
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/rabbitmq/tools/json/JSONWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
import java.util.Map;
import java.util.Set;

/**
* Will be removed in 6.0
* @deprecated Use a third-party JSON library, e.g. Jackson or Gson
*/
public class JSONWriter {
private boolean indentMode = false;
private int indentLevel = 0;
Expand Down
72 changes: 72 additions & 0 deletions src/main/java/com/rabbitmq/tools/jsonrpc/DefaultJsonRpcMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) 2018 Pivotal Software, Inc. All rights reserved.
//
// This software, the RabbitMQ Java client library, is triple-licensed under the
// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2
// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see
// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL,
// please see LICENSE-APACHE2.
//
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
// either express or implied. See the LICENSE file for specific language governing
// rights and limitations of this software.
//
// If you have any questions regarding licensing, please contact us at
// info@rabbitmq.com.

package com.rabbitmq.tools.jsonrpc;

import com.rabbitmq.tools.json.JSONReader;
import com.rabbitmq.tools.json.JSONWriter;

import java.util.List;
import java.util.Map;

/**
* Simple {@link JsonRpcMapper} based on homegrown JSON utilities.
* Handles integers, doubles, strings, booleans, and arrays of those types.
* <p>
* For a more comprehensive set of features, use {@link JacksonJsonRpcMapper}.
* <p>
* Will be removed in 6.0
*
* @see JsonRpcMapper
* @see JacksonJsonRpcMapper
* @since 5.4.0
* @deprecated use {@link JacksonJsonRpcMapper} instead
*/
public class DefaultJsonRpcMapper implements JsonRpcMapper {

@Override
public JsonRpcRequest parse(String requestBody, ServiceDescription description) {
@SuppressWarnings("unchecked")
Map<String, Object> request = (Map<String, Object>) new JSONReader().read(requestBody);
return new JsonRpcRequest(
request.get("id"), request.get("version").toString(), request.get("method").toString(),
((List<?>) request.get("params")).toArray()
);
}

@Override
public JsonRpcResponse parse(String responseBody, Class<?> expectedType) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) (new JSONReader().read(responseBody));
Map<String, Object> error;
JsonRpcException exception = null;
if (map.containsKey("error")) {
error = (Map<String, Object>) map.get("error");
exception = new JsonRpcException(
new JSONWriter().write(error),
(String) error.get("name"),
error.get("code") == null ? 0 : (Integer) error.get("code"),
(String) error.get("message"),
error
);
}
return new JsonRpcResponse(map.get("result"), map.get("error"), exception);
}

@Override
public String write(Object input) {
return new JSONWriter().write(input);
}
}
Loading