ph-ubl icon indicating copy to clipboard operation
ph-ubl copied to clipboard

Support for json

Open Penait1 opened this issue 6 years ago • 12 comments

Are there any plans for json support? Or will it be xml only for now. Just wondering since I really like the library but it would be useful to have support for json aswell.

Penait1 avatar Mar 18 '19 15:03 Penait1

+1, especially useful when communicating with Javascript-based systems

ESchouten avatar May 06 '19 10:05 ESchouten

Okay, noted. Are you aware of any tool support to create the necessary stubs? What do you exactly expect with the JSON? Reading and Writing is pertty obvious, but is there a JSON Schema like representation for validation? Anyone has an implementation hint for me, except for going with the existing JAXB annotations?

Notes to myself: Specs: http://docs.oasis-open.org/ubl/UBL-2.1-JSON/v1.0/UBL-2.1-JSON-v1.0.html Seems like there is no binding for 2.0 and 2.2 (yet)

phax avatar May 06 '19 10:05 phax

As for my expectations of the JSON support, I'd say reading and writing would be the main focus in order to support non-XML supporting clients. Since PH-UBL validates the Java objects, AFAIK, validation could still be performed by the XSD's used currently.

Would simple JSON serializing suffice to map the data directly on the JAXB annotated classes?

Excuse me if my understanding of this library is insufficient to provide a useful answer.

ESchouten avatar May 06 '19 11:05 ESchouten

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Aug 04 '19 11:08 stale[bot]

Hello @phax, Is there any possibility/way to use this library in the REST(JSON) based API framework? The library is too good but I am a bit confused about JSON management in the library.

I am trying to make a Spring Boot-based application that will accept JSON(for now) Invoice/CN/DN/etc. and serialize that data to related Document and validate it and store it into the database.

Can you please help me to understand, how to integrate and use the library in the Spring boot application?

kurbhatt avatar May 07 '21 09:05 kurbhatt

This library does NOT support the creation of JSON, because it is purely working on the JAXB objects and is hence limited to XML. I think the JSON binding proposed at http://docs.oasis-open.org/ubl/UBL-2.3-JSON/v1.0/UBL-2.3-JSON-v1.0.html is ugly and I am not aware of any tool that performs these modifications on the fly. Until that time I suggest you stick with XML - sorry

phax avatar May 07 '21 11:05 phax

Thanks, @phax for the quick response. The main issue is how can I receive XML in API in an application? What is the way to accept UBL XML files in API requests? (As its structure now allowed in POST request(JAXRS/JAXB)) As of now, file upload is the only way. Upload XML file, write it to file system then pass it to UBLReader, after process delete file. Please correct me if I am wrong.

kurbhatt avatar May 07 '21 13:05 kurbhatt

I am confused about what you are writing. You should easily be able to process UBL XML documents per POST with JAXB/JAXRS. Try using the type org.w3c.Document as the parameter to receive arbitrary XML payloads - if not please create a separate ticket with more context. In general JAXRS should be totally content agnostic and take everything...

phax avatar May 07 '21 14:05 phax

I have tried to send sample UBL 2.1 Invoice as a JAXRS with application/xml as a request/response type and org.w3c.dom.Document as parameter but it says 400-Bad Request I am using Spring Boot. Following error on Spring console:

Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `org.w3c.dom.Node` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `org.w3c.dom.Node` out of START_OBJECT token
 at [Source: (PushbackInputStream); line: 2, column: 1]]

kurbhatt avatar May 11 '21 09:05 kurbhatt

As I am not using JAXRS I can only provide you to official sources and not personal experience. Try e.g.

  • https://docs.oracle.com/javaee/6/tutorial/doc/gkknj.html - Using JAX-RS With JAXB
  • IBM Information: https://www.ibm.com/docs/en/was/8.5.5?topic=uxcijrarr-implementing-resource-method-using-jaxb-objects-xml-content
  • May be this one: https://www.logicbig.com/tutorials/java-ee-tutorial/jax-rs/xml-response.html

hth

phax avatar May 11 '21 10:05 phax

Thanks for the details @phax Will figure it out and try to have it in working condition.

kurbhatt avatar May 12 '21 10:05 kurbhatt

@kurbhatt yes please do so. If not, please create a new issue - it is kind of off-topic in this one

phax avatar May 12 '21 10:05 phax

I still don't think JSON really adds value here - it just adds complexity and requires transformations etc. XML is mature, well understood and has good tooling support - I will not add JSON support.

phax avatar Oct 07 '23 10:10 phax