1

I know that fields listed in a json schema object have no defined order, since they are not an array, but I am looking for a way to be able to display them in the proper order in my application UI.

Workarounds I have found so far include things like using a different serializer, or even hard-coding a number into the field name.

I would like to come up with something that works with my current setup. Hibernate, Spring Boot, and a react-app front end. given this GET request:

/profile/personEntities 

with header: Accept: application/schema+json

I will receive this:

{
    "title": "Person entity",
    "properties": {
        "birthday": {
            "title": "Birthday",
            "readOnly": false,
            "type": "string",
            "format": "date-time"
        },
        "lastName": {
            "title": "Last name",
            "readOnly": false,
            "type": "string"
        },
        "address": {
            "title": "Address",
            "readOnly": false,
            "type": "string",
            "format": "uri"
        },
        "firstName": {
            "title": "First name",
            "readOnly": false,
            "type": "string"
        },
        "email": {
            "title": "Email",
            "readOnly": false,
            "type": "string"
        },
        "cellPhone": {
            "title": "Cell phone",
            "readOnly": false,
            "type": "string"
        }
    },
    "requiredProperties": [
        "firstName",
        "lastName"
    ],
    "definitions": {},
    "type": "object",
    "$schema": "http://json-schema.org/draft-04/schema#"
}

I have tried adding @JsonProperty(index=2) to the field, but nothing changes.

Thank you much for any tips.

Llazar
  • 3,167
  • 3
  • 17
  • 24
Airborne Eagle
  • 373
  • 5
  • 11

1 Answers1

1

If you're using Jackson to handle your serialization/deserialization you can use @JsonPropertyOrder - from their docs:

// ensure that "id" and "name" are output before other properties
@JsonPropertyOrder({ "id", "name" })

// order any properties that don't have explicit setting using alphabetic order
@JsonPropertyOrder(alphabetic=true)

See: http://fasterxml.github.io/jackson-annotations/javadoc/2.3.0/com/fasterxml/jackson/annotation/JsonPropertyOrder.html

Markoorn
  • 2,477
  • 1
  • 16
  • 31
  • Thank you. That works when I am making a standard Spring Data Rest Call (eg: /personEntities -> {_embedded: [{firstName:Tester, lastName:McTest}]}), but when I make a call to get the schema (example given in original post), there is no change. I have looked through their code, and I am not sure how Spring is handling the schema data. Any Idea how they are serializing the schema data? – Airborne Eagle Nov 06 '18 at 18:11
  • Oh I see, interesting - I would have thought it would be also honour the @JsonPropertyOrder annotations since spring uses Jackson to handle their json serialization/deserialization. I've been doing some digging and there doesn't seem to be a way to control the serialization order of the schema - see this answer for a potential workaround: https://stackoverflow.com/a/31693656/3368558 – Markoorn Nov 07 '18 at 09:18