com.yahoo.vespa.hosted.provision.persistence.StringSetSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of node-repository Show documentation
Show all versions of node-repository Show documentation
Keeps track of node assignment in a multi-application setup.
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.persistence;
import com.yahoo.slime.ArrayTraverser;
import com.yahoo.slime.Cursor;
import com.yahoo.slime.Inspector;
import com.yahoo.slime.Slime;
import com.yahoo.vespa.config.SlimeUtils;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
/**
* Serialization of a set of strings to/from Json bytes using Slime.
*
* The set is serialized as an array of string.
*
* @author bratseth
*/
public class StringSetSerializer {
public byte[] toJson(Set stringSet) {
try {
Slime slime = new Slime();
Cursor array = slime.setArray();
for (String element : stringSet)
array.addString(element);
return SlimeUtils.toJsonBytes(slime);
}
catch (IOException e) {
throw new RuntimeException("Serialization of a string set failed", e);
}
}
public Set fromJson(byte[] data) {
Inspector inspector = SlimeUtils.jsonToSlime(data).get();
Set stringSet = new HashSet<>();
inspector.traverse((ArrayTraverser) (index, name) -> stringSet.add(name.asString()));
return stringSet;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy