forked from openhab/openhab-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consistent ordering of entries in jsondb (openhab#2437)
* Consistent ordering of entries in jsondb Resolves openhab#2436. Signed-off-by: Sami Salonen <[email protected]>
- Loading branch information
Showing
6 changed files
with
260 additions
and
6 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
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
62 changes: 62 additions & 0 deletions
62
...ab.core.config.core/src/main/java/org/openhab/core/config/core/OrderingMapSerializer.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,62 @@ | ||
/** | ||
* Copyright (c) 2010-2021 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.core.config.core; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.Comparator; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.stream.Stream; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonSerializationContext; | ||
import com.google.gson.JsonSerializer; | ||
|
||
/** | ||
* Serializes map data by ordering the keys | ||
* | ||
* @author Sami Salonen - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class OrderingMapSerializer implements JsonSerializer<Map<@Nullable Object, @Nullable Object>> { | ||
|
||
@SuppressWarnings({ "rawtypes", "unchecked", "null" }) | ||
@Override | ||
public JsonElement serialize(Map<@Nullable Object, @Nullable Object> src, Type typeOfSrc, | ||
JsonSerializationContext context) { | ||
JsonObject ordered = new JsonObject(); | ||
final Stream<Entry<@Nullable Object, @Nullable Object>> possiblySortedStream; | ||
if (OrderingSetSerializer.allSameClassAndComparable(src.keySet())) { | ||
// Map keys are comparable as verified above so casting to plain Comparator is safe | ||
possiblySortedStream = src.entrySet().stream().sorted((Comparator) Map.Entry.comparingByKey()); | ||
} else { | ||
possiblySortedStream = src.entrySet().stream(); | ||
} | ||
possiblySortedStream.forEachOrdered(entry -> { | ||
Object key = entry.getKey(); | ||
if (key instanceof String) { | ||
ordered.add((String) key, context.serialize(entry.getValue())); | ||
} else { | ||
JsonElement serialized = context.serialize(key); | ||
ordered.add(serialized.isJsonPrimitive() ? serialized.getAsString() : serialized.toString(), | ||
context.serialize(entry.getValue())); | ||
} | ||
|
||
}); | ||
return ordered; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...ab.core.config.core/src/main/java/org/openhab/core/config/core/OrderingSetSerializer.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,64 @@ | ||
/** | ||
* Copyright (c) 2010-2021 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.core.config.core; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.Set; | ||
import java.util.stream.Stream; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
|
||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonSerializationContext; | ||
import com.google.gson.JsonSerializer; | ||
|
||
/** | ||
* Serializes set by ordering the elements | ||
* | ||
* @author Sami Salonen - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class OrderingSetSerializer implements JsonSerializer<Set<@Nullable Object>> { | ||
|
||
public static boolean allSameClassAndComparable(Set<@Nullable Object> src) { | ||
Class<?> expectedClass = null; | ||
for (Object object : src) { | ||
if (!(object instanceof Comparable<?>)) { | ||
// not comparable or simply null | ||
return false; | ||
} else if (expectedClass == null) { | ||
// first item | ||
expectedClass = object.getClass(); | ||
} else if (!object.getClass().equals(expectedClass)) { | ||
// various classes in the Set, let's not try to sort | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public JsonElement serialize(Set<@Nullable Object> src, Type typeOfSrc, JsonSerializationContext context) { | ||
JsonArray ordered = new JsonArray(); | ||
final Stream<@Nullable Object> possiblySortedStream; | ||
if (allSameClassAndComparable(src)) { | ||
possiblySortedStream = src.stream().sorted(); | ||
} else { | ||
possiblySortedStream = src.stream(); | ||
} | ||
possiblySortedStream.map(context::serialize).forEachOrdered(ordered::add); | ||
return ordered; | ||
} | ||
} |
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
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