Skip to content
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

JaVers objects should be Jackson friendly #205

Closed
igler opened this issue Sep 22, 2015 · 10 comments
Closed

JaVers objects should be Jackson friendly #205

igler opened this issue Sep 22, 2015 · 10 comments

Comments

@igler
Copy link

igler commented Sep 22, 2015

Serialize JaVers changes List<Change> through Jackson. Snapshots List<CdoSnapshot>already work.

@bartoszwalacik
Copy link
Member

today I'll release javers 1.3.9, int this version
exception AFFECTED_CDO_IS_NOT_AVAILABLE JaVers runtime error when serializing Changes to JSON using Jackson is fixed.

Still, JSON output created by Jackson will be very verbose as it simply uses Jackson defaults.
Neat nad consice JSON serialization of JaVers objects can be easily done using JaVers internal JSON mapping engine (based on Gson) - javers.getJsonConverter().toJson(...)

@igler
Copy link
Author

igler commented Sep 24, 2015

Sounds good. I'll stay tuned and let you know how things go.

@bartoszwalacik
Copy link
Member

check fresh 1.3.9 release

@igler
Copy link
Author

igler commented Sep 25, 2015

Works perfect! Using the following code JSON-output is nice and smooth:

@RequestMapping(value = "/{id:[0-9]+}" + ChangeLinks.CHANGES, method = RequestMethod.GET)
public ResponseEntity<List<Change>> getChangesOfLocation(@PathVariable("id") Location location) {
        
    List changeList = javers
        .findChanges(QueryBuilder.byInstanceId(location.getId(), PublicLocation.class).build());

BTW, how can I influence how changes on references are rendered in "left" and "right" properties? Does it eventually work with Spring data-projections?

@bartoszwalacik
Copy link
Member

well, since you are not using JaVavers to build JSON, it's rather a question to spring-mcv or jackson,
so it's on your side.

That's why I recommend using Javers to building JSON. JaVers creates neat, concise and customizable JSON.

In Spring controller you can do it quite easily:

@RequestMapping(...)
@ResponseBody
public String getChanges() {
    List changeList = javers.findChanges(...);
    return javers.getJsonConverter().toJson(changeList);
}

@igler
Copy link
Author

igler commented Sep 25, 2015

What I meant was adjusting the following output through e.g. a CustomPropertyComparator (http://javers.org/documentation/diff-configuration/):

"left": {
      "cdoId": 2,
      "cdoClass": {
        "clientsClass": "....LocationNetwork",
        "idProperty": {
          "name": "id",
          "type": "java.lang.Long",
          "genericType": "java.lang.Long",
          "hasTransientAnn": false
        }
        ...
   ...

Is it possible to retrieve the name of the LocationNetwork object and place it inside the "left"-property as a string?

@igler
Copy link
Author

igler commented Sep 25, 2015

... did the following:

public class LocationNetworkComparator implements CustomPropertyComparator<LocationNetwork, ValueChange> {
    @Override
    public ValueChange compare(LocationNetwork leftNetwork, LocationNetwork rightNetwork, GlobalId globalId, Property property) {
        String leftNetworkName = leftNetwork.getName();
        String rightNetworkName = rightNetwork.getName();
        return new ValueChange(globalId, property, leftNetworkName, rightNetworkName);
    }
}

... which works when I change a property only (of course as the upper comparator is not involved) or change a reference only. But when I do both in combination it comes to an error when the name of the reference (network) is resolved. In this case leftNetwork and rightNetwork are bot null.

@igler
Copy link
Author

igler commented Sep 25, 2015

OK, got it now. It is important that the comparator code is written the following way when the third situation occurs:

public class LocationNetworkComparator implements CustomPropertyComparator<LocationNetwork, ValueChange> {
    @Override
    public ValueChange compare(LocationNetwork leftNetwork, LocationNetwork rightNetwork, GlobalId globalId, Property property) {
        if(leftNetwork != null && rightNetwork != null && leftNetwork.equals(rightNetwork))
            return null;
        else {
            String leftNetworkName = null;
            String rightNetworkName = null;
            if (leftNetwork != null)
                leftNetworkName = leftNetwork.getName();
            if (rightNetwork != null)
                rightNetworkName = rightNetwork.getName();
            return new ValueChange(globalId, property, leftNetworkName, rightNetworkName);
        }
    }
}

@bartoszwalacik
Copy link
Member

Ok, good to hear that CustomComparators works for you

@igler
Copy link
Author

igler commented Sep 26, 2015

You can close this issue if you want to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants