1

Smile is compact representation of JSON in binary format that represents JSON data in a more efficient manner than regular text-based JSON. The idea is leverage the use of binary JSON to cut down on the amount of data communicated between client and server.

On the server side, FasterXML/jackson Java lib is used to return JSON data in Smile format (binary JSON as follows:

    OutputStream os = response.getOutputStream();
    SmileFactory factory = new SmileFactory();
    SmileGenerator generator = (SmileGenerator) 
    factory.createGenerator(os);
    generator.writeStartObject();
    generator.writeFieldName("sEcho");
    generator.writeString(sEcho);
    ...
    generator.writeEndObject();
    generator.close();

When attempting to switch Datatables to use binary JSON as per recommendation, the ajax option to Datatables construction was added as per below, but error message "Invalid JSON response" is outputted on load of data-tables. Any ideas how to resolve?

 "ajax": {
                    "url": "fetchtabledata.do?" + "instance=<c:out value="${resultsBean.instance}"/>",
                    "dataType": "binary",
                    "dataSrc": function ( json ) {
                        return Smile.Parser.parse(json);
                    }
            }, 
jamie
  • 672
  • 1
  • 9
  • 17
  • Can you [edit] your question to clarify what you mean by "binary JSON"? How is such data created? Can you provide an example? – andrewJames Oct 25 '20 at 14:24
  • There is no `binary` MIME type. Binary data can have a variety of possible MIME types depending on how the data was created. The generic [MIME type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types) for binary data is `application/octet-stream`. But whatever you use, you have to decode it back to valid textual JSON for DataTables to be able to manipulate it. – andrewJames Oct 25 '20 at 14:27
  • Edited above. You can see in the code, I am using the Smile Javascript lib to parse the binary JSON data and return it to datatables. Unfortunately, this method is never executed due to the "invalid json error". The dataType binary was taken from Jquery ajax parameters. – jamie Oct 26 '20 at 15:42
  • Thank you for the edits. Unfortunately, now I am hitting the same problem as you. Even with the jQuery ajax `dataFilter` option, to pre-process the response, it is still throwing a parse error (but the byte array is being received correctly from the server). – andrewJames Oct 26 '20 at 22:35

0 Answers0