| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.velocity.tools; |
| 21 |
|
|
| 22 |
|
import java.io.IOException; |
| 23 |
|
|
| 24 |
|
import org.apache.commons.lang3.StringUtils; |
| 25 |
|
import org.json.JSONArray; |
| 26 |
|
import org.json.JSONObject; |
| 27 |
|
import org.slf4j.Logger; |
| 28 |
|
import org.slf4j.LoggerFactory; |
| 29 |
|
|
| 30 |
|
import com.fasterxml.jackson.core.JsonGenerator; |
| 31 |
|
import com.fasterxml.jackson.core.JsonProcessingException; |
| 32 |
|
import com.fasterxml.jackson.core.Version; |
| 33 |
|
import com.fasterxml.jackson.databind.JsonSerializer; |
| 34 |
|
import com.fasterxml.jackson.databind.ObjectMapper; |
| 35 |
|
import com.fasterxml.jackson.databind.SerializerProvider; |
| 36 |
|
import com.fasterxml.jackson.databind.module.SimpleModule; |
| 37 |
|
|
| 38 |
|
import net.sf.json.JSON; |
| 39 |
|
import net.sf.json.JSONException; |
| 40 |
|
import net.sf.json.JSONSerializer; |
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
@version |
| 46 |
|
@since |
| 47 |
|
|
| |
|
| 86.7% |
Uncovered Elements: 2 (15) |
Complexity: 4 |
Complexity Density: 0.31 |
|
| 48 |
|
public class JSONTool |
| 49 |
|
{ |
| 50 |
|
|
| 51 |
|
private Logger logger = LoggerFactory.getLogger(JSONTool.class); |
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
@param |
| 66 |
|
@return |
| 67 |
|
|
| |
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
|
| 68 |
643 |
public String serialize(Object object)... |
| 69 |
|
{ |
| 70 |
643 |
try { |
| 71 |
643 |
ObjectMapper mapper = new ObjectMapper(); |
| 72 |
643 |
SimpleModule m = new SimpleModule("org.json.* serializer", new Version(1, 0, 0, "", "org.json", "json")); |
| 73 |
643 |
m.addSerializer(JSONObject.class, new JSONObjectSerializer()); |
| 74 |
643 |
m.addSerializer(JSONArray.class, new JSONArraySerializer()); |
| 75 |
643 |
mapper.registerModule(m); |
| 76 |
643 |
return mapper.writeValueAsString(object); |
| 77 |
|
} catch (JsonProcessingException e) { |
| 78 |
0 |
this.logger.error("Failed to serialize object to JSON", e); |
| 79 |
|
} |
| 80 |
|
|
| 81 |
0 |
return null; |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
|
| 88 |
|
@param |
| 89 |
|
@return@link@link |
| 90 |
|
|
| 91 |
|
@since |
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 95 |
11 |
public JSON parse(String json)... |
| 96 |
|
{ |
| 97 |
11 |
try { |
| 98 |
11 |
return JSONSerializer.toJSON(json); |
| 99 |
|
} catch (JSONException ex) { |
| 100 |
2 |
this.logger.info("Tried to parse invalid JSON: [{}], exception was: {}", StringUtils.abbreviate(json, 32), |
| 101 |
|
ex.getMessage()); |
| 102 |
2 |
return null; |
| 103 |
|
} |
| 104 |
|
} |
| 105 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
| 106 |
|
class JSONObjectSerializer extends JsonSerializer<JSONObject> |
| 107 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 108 |
2 |
@Override... |
| 109 |
|
public void serialize(JSONObject value, JsonGenerator jgen, SerializerProvider provider) |
| 110 |
|
throws IOException, JsonProcessingException |
| 111 |
|
{ |
| 112 |
2 |
jgen.writeRawValue(value.toString()); |
| 113 |
|
} |
| 114 |
|
} |
| 115 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
| 116 |
|
class JSONArraySerializer extends JsonSerializer<JSONArray> |
| 117 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 118 |
2 |
@Override... |
| 119 |
|
public void serialize(JSONArray value, JsonGenerator jgen, SerializerProvider provider) |
| 120 |
|
throws IOException, JsonProcessingException |
| 121 |
|
{ |
| 122 |
2 |
jgen.writeRawValue(value.toString()); |
| 123 |
|
} |
| 124 |
|
} |
| 125 |
|
} |