[
https://issues.apache.org/jira/browse/KARAF-5793?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16603915#comment-16603915
]
ASF GitHub Bot commented on KARAF-5793:
---------------------------------------
jbonofre closed pull request #40: KARAF-5793 Provide an option replace the dot
or not in json field in …
URL: https://github.com/apache/karaf-decanter/pull/40
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/marshaller/json/src/main/java/org/apache/karaf/decanter/marshaller/json/JsonMarshaller.java
b/marshaller/json/src/main/java/org/apache/karaf/decanter/marshaller/json/JsonMarshaller.java
index 9bae949..79347df 100644
---
a/marshaller/json/src/main/java/org/apache/karaf/decanter/marshaller/json/JsonMarshaller.java
+++
b/marshaller/json/src/main/java/org/apache/karaf/decanter/marshaller/json/JsonMarshaller.java
@@ -21,6 +21,7 @@
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
+import java.util.Dictionary;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@@ -34,11 +35,14 @@
import javax.json.JsonWriter;
import org.apache.karaf.decanter.api.marshaller.Marshaller;
+import org.osgi.service.component.ComponentContext;
+import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventConstants;
@Component(
+ name = "org.apache.karaf.decanter.marshaller.json",
immediate = true,
property = Marshaller.SERVICE_KEY_DATAFORMAT + "=json"
)
@@ -46,11 +50,20 @@
private SimpleDateFormat tsFormat;
+ boolean replaceDotsByUnderscores = true;
+
public JsonMarshaller() {
tsFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss,SSSX");
tsFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
}
+ @Activate
+ public void activate(ComponentContext componentContext) {
+ Dictionary<String, Object> config = componentContext.getProperties();
+ replaceDotsByUnderscores = (config.get("replaceDotsByUnderscores") !=
null) ?
+ Boolean.valueOf((String) config.get("replaceDotsByUnderscores")) :
true;
+ }
+
@Override
public void marshal(Object obj, OutputStream out) {
JsonObject jsonObj = marshal((Event)obj);
@@ -69,7 +82,8 @@ private JsonObject marshal(Event event) {
addTimestamp(event, json);
for (String key : event.getPropertyNames()) {
Object value = event.getProperty(key);
- marshalAttribute(json, key.replace('.','_'), value);
+ key = replaceDotsByUnderscores ? key.replace('.','_') : key;
+ marshalAttribute(json, key, value);
}
return json.build();
}
@@ -82,7 +96,7 @@ private void addTimestamp(Event event, JsonObjectBuilder
json) {
@SuppressWarnings("unchecked")
private void marshalAttribute(JsonObjectBuilder jsonObjectBuilder, String
key, Object value) {
- key = key.replace('.', '_');
+ key = replaceDotsByUnderscores ? key.replace('.', '_') : key;
if (value instanceof Map) {
jsonObjectBuilder.add(key, build((Map<String, Object>)value));
} else if (value instanceof List) {
@@ -125,7 +139,8 @@ private void marshalAttribute(JsonObjectBuilder
jsonObjectBuilder, String key, O
private JsonObject build(Map<String, Object> value) {
JsonObjectBuilder json = Json.createObjectBuilder();
for (Entry<String, Object> entries : value.entrySet()) {
- addProperty(json, entries.getKey().replace('.','_'),
entries.getValue());
+ addProperty(json, replaceDotsByUnderscores?
entries.getKey().replace('.','_') :
+ entries.getKey(), entries.getValue());
}
return json.build();
}
@@ -162,7 +177,7 @@ private void addValue(JsonArrayBuilder json, Object value) {
}
private void addProperty(JsonObjectBuilder json, String key, Object value)
{
- key = key.replace('.','_');
+ key = replaceDotsByUnderscores ? key.replace('.','_') : key;
if (value instanceof BigDecimal) {
json.add(key, (BigDecimal)value);
} else if (value instanceof BigInteger) {
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> Provide an option replace the dot or not in json field in the JsonMarshaller
> ----------------------------------------------------------------------------
>
> Key: KARAF-5793
> URL: https://issues.apache.org/jira/browse/KARAF-5793
> Project: Karaf
> Issue Type: Improvement
> Components: decanter
> Affects Versions: decanter-2.0.0
> Reporter: Xilai Dai
> Assignee: Jean-Baptiste Onofré
> Priority: Major
> Fix For: decanter-2.1.0
>
>
> The dot "." is replaced by "_" in json fields after changes from KARAF-4295.
> The reason could be that the field names containing dots will be rejected in
> ES 2.x (https://github.com/elastic/elasticsearch/issues/15951). but ES 5.x,
> 6.x support it again (parse it as Object). see
> https://www.elastic.co/guide/en/elasticsearch/reference/2.4/dots-in-names.html#_enabling_support_for_dots_in_field_names
> . Also there is an ENV option provided by ES to allow dots in the field
> name:
> {code}
> export ES_JAVA_OPTS="-Dmapper.allow_dots_in_name=true"
> {code}
> To give more compatibility and flexibility to the user, we may provide an
> option "replaceDotInFields=true/false" for the Json Marshaller. for now any
> custom fields name with "." woud be replaced by "_" silently.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)