Skip to content

Conversation

@mmindenhall
Copy link

Hi Igor,

I hope you'll accept this one into the core library. I think being able to convert the parsed data structure hierarchy to other formats (e.g., JSON, XML) is essential for many use cases. Most JSON libraries can convert from Map<String, Object> to JSON, and there are also libraries to convert from Map<String, Object> to XML. For example, I was able to easily do the following using the jackson library:

    final InputStream pngStream = getResourceAsInputStream("picture.png");
    try {
      final JBBPParser pngParser = JBBPParser.prepare(
              "long header;"
              + "// chunks\n"
              + "chunk [_]{"
              + "   int length; "
              + "   int type; "
              + "   byte[length] data; "
              + "   int crc;"
              + "}"
      );

      final JBBPFieldStruct result = pngParser.parse(pngStream);
      final Map<String, Object> resultMap = result.asMap();

      ObjectMapper mapper = new ObjectMapper();
      JsonNode jsonNode = mapper.valueToTree(resultMap);
      // do something with the JsonNode
    }
    finally {
      JBBPUtils.closeQuietly(pngStream);
    }
  }

Also note that these changes only add 3,859 bytes to the size of the library (151,686 vs 147,827).

Thanks!

@raydac
Copy link
Owner

raydac commented May 21, 2015

I can't include such code into the library because it has specific purposes and cases, of course many products may be have such needs but in the case I guess should be developed something like JBBP-JSON or JBBP-XML libraries with specific codes and may be even adapted for specific JSON libraries (because there are a lot of such libraries), the main purpose of JBBP - parse binary data, it should not know about something behind that, take a look at how it can be implemented conversion of PNG to JSON in 100 lines

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