public static JsonObject serialize(ContextSet contextSet) {
        JsonObject output = new JsonObject();

        List<Map.Entry<String, Set<String>>> entries = new ArrayList<>(contextSet.toMap().entrySet());
        entries.sort(Map.Entry.comparingByKey()); // sort - consistent output order

        for (Map.Entry<String, Set<String>> entry : entries) {
            String[] values = entry.getValue().toArray(new String[0]);
            switch (values.length) {
                case 0:
                    break;
                case 1:
                    output.addProperty(entry.getKey(), values[0]);
                    break;
                default:
                    Arrays.sort(values); // sort - consistent output order
                    JsonArray arr = new JsonArray();
                    for (String value : values) {
                        arr.add(new JsonPrimitive(value));
                    }
                    output.add(entry.getKey(), arr);
                    break;
            }
        }

        return output;
    }
Serializes and deserializes {@link ContextSet}s to and from JSON.

<p>The entries within the serialized output are sorted, this ensures that any two invocations
of {@link #serialize(ContextSet)} with the same {@link ContextSet} will produce
the same exact JSON string.</p>
common/src/main/java/me/lucko/luckperms/common/context/serializer/ContextSetJsonSerializer.java