-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(#2106) Serialize non-primitive types as string and store in Influ…
…xDB (#2196) * Serialize non-primitive types and store in Influx * extract RawFieldSerializer * rename test * delete old
- Loading branch information
Showing
3 changed files
with
162 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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
53 changes: 53 additions & 0 deletions
53
...ava/org/apache/streampipes/dataexplorer/commons/influx/serializer/RawFieldSerializer.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF 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 org.apache.streampipes.dataexplorer.commons.influx.serializer; | ||
|
||
import org.apache.streampipes.commons.exceptions.SpRuntimeException; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator; | ||
|
||
public class RawFieldSerializer { | ||
protected ObjectMapper objectMapper; | ||
|
||
public RawFieldSerializer() { | ||
this.objectMapper = new ObjectMapper().activateDefaultTyping( | ||
BasicPolymorphicTypeValidator.builder() | ||
.allowIfBaseType(Object.class) | ||
.build(), | ||
ObjectMapper.DefaultTyping.EVERYTHING); | ||
} | ||
|
||
public String serialize(Object object) { | ||
try { | ||
return objectMapper.writeValueAsString(object); | ||
} catch (JsonProcessingException e) { | ||
throw new SpRuntimeException(e.getCause()); | ||
} | ||
} | ||
|
||
public Object deserialize(String json) { | ||
try { | ||
return objectMapper.readValue(json, Object.class); | ||
} catch (JsonProcessingException e) { | ||
throw new SpRuntimeException(e.getCause()); | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
...org/apache/streampipes/dataexplorer/commons/influx/serializer/TestRawFieldSerializer.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF 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 org.apache.streampipes.dataexplorer.commons.influx.serializer; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class TestRawFieldSerializer { | ||
private RawFieldSerializer rawFieldSerializer = new RawFieldSerializer(); | ||
private Map<String, Object> primitives = new HashMap<String, Object>(); | ||
|
||
public TestRawFieldSerializer() { | ||
primitives.put("Integer", 1); | ||
primitives.put("Long", 1L); | ||
primitives.put("Float", 1.0f); | ||
primitives.put("Double", 1.0d); | ||
primitives.put("Boolean", true); | ||
primitives.put("String", "1"); | ||
} | ||
|
||
// Test able to deserialize back the original data | ||
@Test | ||
public void testRawFieldSerializerListInMap() { | ||
var rawListField = new ArrayList<Object>(); | ||
rawListField.addAll(primitives.values()); | ||
|
||
var rawNestedField = new HashMap<String, Object>(); | ||
rawNestedField.putAll(primitives); | ||
rawNestedField.put("List", rawListField); | ||
|
||
var json = rawFieldSerializer.serialize(rawNestedField); | ||
|
||
assertEquals(rawNestedField, rawFieldSerializer.deserialize(json)); | ||
} | ||
|
||
@Test | ||
public void testRawFieldSerializerMapInList() { | ||
var rawNestedField = new HashMap<String, Object>(); | ||
rawNestedField.putAll(primitives); | ||
|
||
var rawListField = new ArrayList<Object>(); | ||
rawListField.addAll(primitives.values()); | ||
rawListField.add(rawNestedField); | ||
|
||
var json = rawFieldSerializer.serialize(rawListField); | ||
|
||
assertEquals(rawListField, rawFieldSerializer.deserialize(json)); | ||
} | ||
} |