- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 2k
Let JsonFormatter implement Serializer #3102
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
base: main
Are you sure you want to change the base?
Conversation
Once in a while I need to write json output in some of my plugins as well but it is surprisingly hard to do so as the API to do so is all internal and non trivial to implement. On the other hand the JsonFormatter already has everything we need here, so this lets JsonFormatter now implement Serializer to conviently support this use-case
| Would the   Though admittedly I'm a bit confused about what your use case is exactly. 
 Here it sounds like you want to write arbitrary json. But the  | 
| 
 Good catch, that would be a better place to implement that interface But it has the same limitations as JsonFormatter: 
 
 No, I want exactly that: 
 So this hopefully makes it more clear: 
 This all is used to observer the test-run of a forked cucumber test in eclipse-cucumber plugin. | 
| Unfortunately Java doesn't come with a builtin JSON serializer/deserializer.  The  I don't mean to XY-problem you, but it really seems like you need a JSON serializer/deserializer. What is the reason you can't bring your own serializer? 
 I did see you were working on something similar for JUnit (junit-team/junit-framework#5096, ota4j-team/open-test-reporting#728). There it seems writing to an output stream is sufficient? But for what it is worth, the  ByteArrayOutputStream delegate = new ByteArrayOutputStream();
OutputStream outputStream = new OutputStream() {
    private final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    @Override
    public void write(int b) throws IOException {
        if (b == '\n') {
            delegate.write(buffer.size());
            buffer.writeTo(delegate);
            buffer.reset();
        } else {
            buffer.write(b);
        }
    }
};
MessageFormatter messageFormatter = new MessageFormatter(outputStream); | 
| Alternatively would it help if messages implemented the serializable interface? | 
| 
 That's why I found it quite smart if the ones here implement the Serializer interface (same for HTml of course by the way). 
 The plugin should be as minimal as possible (as I don't know what people are using) and I don't want to bring in something new, and as cucumber already support this and I want exactly this it seems wasted effort anyways. 
 The protocol requires an ACK (so I can hold on the remote process for debugging steps), the formatters only write and forget (what is good!) so I really need to know the boundaries. Also i probably want to send other commands over the channel and the writer possibly do not give me enough control (e.g about flush). 
 So as said its all possible somehow but not very convenient and I'm bound to how the internal implementation works and I'm not sure I want to put to much burden on it. To be concrete, would you say it will always be the case now and in the future?! And always a single  
 Not really as then it would require exact same class versions on client/server. | 
| Would this be sufficient? public final class ObjectToJsonWriter {
    
    @Override
    public void writeValue(Writer writer, Object value) throws IOException {
        Jackson.OBJECT_MAPPER.writeValue(writer, value);
    }
} | 
| @mpkorstanje yes that would of course be suitable as well, I just noticed that in this case an output stream might be more flexible? (and one maybe want to use that in the Message/Jsonformater then as well for consistency). it would of course be great to have the reverse as well for symmetry: Thinking further in the sense of the API consistency it might be better to simply have a class implement 
 this would also make clear that we write/get an Envelope here... | 
| I'm going to think about it for a bit. Providing a json serializer/deserializer has a lot of complexity attached. And I'm worried that it could open a maintaince sinkhole. | 
Once in a while I need to write json output in some of my plugins as well but it is surprisingly hard to do so as the API to do so is all internal and non trivial to implement.
On the other hand the JsonFormatter already has everything we need here, so this lets JsonFormatter now implement Serializer to conviently support this use-case
@mpkorstanje what do you think? I just (again) encountered the problem that my custom hack for this broke the Eclipse-Cucumber plugin, having a semi official API for that would be great and it does not exposes to much of the internals but still allow to reliable write out the JSON (e.g. to a file or websocket) in custom plugins.