Error Converting jcas object to json object (Infinite recursion)

115 Views Asked by At

I am trying to convert a JCas object (org.apache.uima.jcas.JCas) to json string using jackson ObjectMapper as following.

public Map<String, List<CuiResponse>> process(final String text) throws ServletException {
        JCas jcas = null;
        Map<String, List<CuiResponse>> resultMap = null;
        if (text != null) {
            try {
                jcas = _pool.getJCas(-1);
                jcas.setDocumentText(text);
                _engine.process(jcas);

                ObjectMapper mapper = new ObjectMapper();
                String jsonString = mapper.writeValueAsString(jcas);


                resultMap = formatResults(jcas);
                _pool.releaseJCas(jcas);
            } catch (Exception e) {
                throw new ServletException(e);
            }
        }
        return resultMap;
    }

But I am getting Infinite recursion exception as below

"timestamp": "2020-02-04T10:41:33.342+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "Infinite recursion (StackOverflowError) (through reference chain: org.apache.uima.cas.impl.TypeSystemImpl[\"features\"]->org.apache.uima.cas.impl.FeatureImpl[\"range\"]->org.apache.uima.cas.impl.TypeImpl[\"typeSystem\"]->org.apache.uima.cas.impl.TypeSystemImpl[\"features\"]->org.apache.uima.cas.impl.FeatureImpl[\"range\"]->org.apache.uima.cas.impl.TypeImpl[\"typeSystem\"]->org.apache.uima.cas.impl.TypeSystemImpl[\"features\"]->org.apache.uima.cas.impl.FeatureImpl[\"range\"]->org.apache.uima.cas.impl.TypeImpl[\"typeSystem\"]->org.apache.uima.cas.impl.TypeSystemImpl[\"features\"]->org.apache.uima.cas.impl.FeatureImpl[\"range\"]->org.apache.uima.cas.impl.TypeImpl[\"typeSystem\"]->org.apache.uima.cas.impl.TypeSystemImpl[\"features\"]->org.apache.uima.cas.impl.FeatureImpl[\"range\"]->org.apache.uima.cas.impl.TypeImpl[\"typeSystem\"]->org.apache.uima.cas.impl.TypeSystemImpl[\"features\"]->org.apache.uima.cas.impl.FeatureImpl[\"range\"]->org.apache.uima.cas.impl.TypeImpl[\"typeSystem\"]->org

Can anybody suggest a solution?

1

There are 1 best solutions below

3
rec On BEST ANSWER

UIMA CAS objects are not designed to be directly serializable by standard mechanisms such as Java Serialization, Jackson or similar libraries.

But the UIMA framework itself knows how to convert a CAS object to/from a number of serialization formats (some write-only):

  • UIMA CAS XMI (read/write)
  • UIMA Binary CAS (several variations, read/write)
  • Java Serialized CAS (through an indirection)
  • UIMA CAS JSON (write-only)
  • UIMA XCAS (read/write)

For more information on supported formats, check out CasIOUtils.