-
Notifications
You must be signed in to change notification settings - Fork 282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use LinkedHashMap to preserve order of schema elements #395
Comments
Hello Josh, I'm not strongly against the ordering, but I find using |
|
Hey Josh, not sure if this will help you but this is how I solved this problem: import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import org.everit.json.schema.ObjectSchema;
import org.everit.json.schema.Schema;
public class OrderedObjectSchema extends ObjectSchema {
public static class Builder extends ObjectSchema.Builder {
private final Map<String, Schema> orderedPropertySchemas = new LinkedHashMap<>();
public Builder addOrderedPropertySchema(String propName, Schema schema) {
super.addPropertySchema(propName, schema);
orderedPropertySchemas.put(propName, schema);
return this;
}
@Override
public OrderedObjectSchema build() {
return new OrderedObjectSchema(this);
}
}
private final Map<String, Schema> orderedPropertySchemas;
/**
* Constructor.
*
* @param builder the builder object containing validation criteria
*/
public OrderedObjectSchema(Builder builder) {
super(builder);
orderedPropertySchemas = Collections.unmodifiableMap(builder.orderedPropertySchemas);
}
@Override
public Map<String, Schema> getPropertySchemas() {
return orderedPropertySchemas;
}
} |
@hiloboy0119 Thanks -- looks about right to me at first glance. I'll give it a shot when I get a chance. |
@hiloboy0119 I think that would be okay as a workaround but I still say it should be updated in the library itself as preserving order is better than random order. I'll make a PR at some point. |
Currently a
HashMap
is used, so the order of e.g. properties is not preserved. A simple change to useLinkedHashMap
instead would preserve the schema designer's insertion order.Example:
The result in something like ...
... where the properties are in an indeterminate order (based on the hash of the key). Preferably, the order would be preserved:
Some would argue "no way man, the JSON spec specifically says that property order doesn't matter" (yeah, I lost that fight and here I am again for round 2), but I'd argue that the order being effectively random is far worse than the order being preserved.
The text was updated successfully, but these errors were encountered: