Skip to content

added jgen.close() in serialize() #24

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

Closed
wants to merge 1 commit into from
Closed

added jgen.close() in serialize() #24

wants to merge 1 commit into from

Conversation

mingfai
Copy link

@mingfai mingfai commented Jan 14, 2015

e.g.for a patch of '{a:1}' to '{a:2}', the patch is

[{"op":"replace","path":"/a","value":2}]

without jgen.close() in serialize(), it will print out:

[{"op":"replace","path":"/a","value":2

should jgen be closed inside serialize()? or outsider by the caller?
please feel free to close and ignore this issue if it should be the later case. I can't say closing it inside serialize() is better, but probably will behave as people expect. thx

@fge
Copy link
Collaborator

fge commented Jan 14, 2015

Uh... Can you show the code you are using?

@mingfai
Copy link
Author

mingfai commented Jan 14, 2015

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.fge.jsonpatch.JsonPatch;
import com.github.fge.jsonpatch.diff.JsonDiff;
import org.junit.Test;

import java.io.IOException;
import java.io.StringWriter;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class JsonPatchTest {
    final ObjectMapper jackson = new ObjectMapper();

    @Test public void testDiff() throws IOException {
        JsonNode json0 = jackson.readTree("{\"a\":1}");
        JsonNode json1 = jackson.readTree("{\"a\":2}");
        JsonPatch patch = JsonDiff.asJsonPatch(json0, json1);
        String diff = jackson.writeValueAsString(patch);
        System.out.println("diff: " + diff);
        assertThat(diff, is("[{\"op\":\"replace\",\"path\":\"/a\",\"value\":2}]"));

        StringWriter out = new StringWriter();
        JsonGenerator jg = jackson.getFactory().createGenerator(out);
        patch.serialize(jg, jackson.getSerializerProvider());
        System.out.println("out: " + out.toString()); //missed "}]"
        //jg.close(); //uncomment will fix
        assertThat(out.toString(), is(diff));
    }
}

this test case fails.

@fge
Copy link
Collaborator

fge commented Jan 14, 2015

Uuuh... That is a strange way to serialize. I have never used that.

What is more, what is the purpose of testing the JSON output? Note that there is no guarantee at all that the members will appear in the order you put here.

Anyway, I don't think it is advisable to close the generator here; what if you serialize a JSON Patch within another POJO?

@mingfai
Copy link
Author

mingfai commented Jan 15, 2015

thx for the clarification, we may close this issue.

Is there a better way to JsonPatch to JSON string? I'm quite new to this project, and looked at the API and there ain't seem to be other option then using the serialize() method. (and when I use it, i notice the "}]" at the end is missing. it may be a good idea to document the usage, btw)

p.s. the test case is just for illustrate the missing "}]", it is as same as removing the assertion.

@mingfai mingfai closed this Jan 22, 2015
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

Successfully merging this pull request may close these issues.

2 participants