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
Resolves openhab#2436. Signed-off-by: Sami Salonen <ssalonen@gmail.com>
- Loading branch information
Showing
7 changed files
with
200 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
42 changes: 42 additions & 0 deletions
42
...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,42 @@ | ||
/** | ||
* 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 org.eclipse.jdt.annotation.NonNullByDefault; | ||
|
||
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<String, Object>> { | ||
|
||
@Override | ||
public JsonElement serialize(Map<String, Object> src, Type typeOfSrc, JsonSerializationContext context) { | ||
JsonObject ordered = new JsonObject(); | ||
src.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey)).forEachOrdered(entry -> { | ||
ordered.add(entry.getKey(), context.serialize(entry.getValue())); | ||
}); | ||
return ordered; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...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,39 @@ | ||
/** | ||
* 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 org.eclipse.jdt.annotation.NonNullByDefault; | ||
|
||
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<Object>> { | ||
|
||
@Override | ||
public JsonElement serialize(Set<Object> src, Type typeOfSrc, JsonSerializationContext context) { | ||
JsonArray ordered = new JsonArray(); | ||
src.stream().map(context::serialize).sorted().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
45 changes: 45 additions & 0 deletions
45
...src/main/java/org/openhab/core/storage/json/internal/OrderingConfigurationSerializer.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,45 @@ | ||
/** | ||
* 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.storage.json.internal; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.Comparator; | ||
import java.util.Map; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.openhab.core.config.core.Configuration; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonSerializationContext; | ||
import com.google.gson.JsonSerializer; | ||
import com.google.gson.internal.LinkedTreeMap; | ||
|
||
/** | ||
* Serializes Configuration object with properties ordered | ||
* | ||
* @author Sami Salonen - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class OrderingConfigurationSerializer implements JsonSerializer<Configuration> { | ||
|
||
@Override | ||
public JsonElement serialize(Configuration src, Type typeOfSrc, JsonSerializationContext context) { | ||
Map<String, Object> orderedProperties = new LinkedTreeMap<>(); | ||
src.getProperties().entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey)) | ||
.forEachOrdered(entry -> { | ||
orderedProperties.put(entry.getKey(), entry.getValue()); | ||
}); | ||
|
||
return context.serialize(new Configuration(orderedProperties)); // XXX: re-normalizes unnecessarily | ||
} | ||
} |
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