예제 #1
0
 public void testMappedJettison() throws Exception {
   System.out.println("\nTesting Mapped Jettison: ---------------------");
   Map<String, String> ns2json = new HashMap<String, String>();
   ns2json.put("http://www.w3.org/2001/XMLSchema-instance", "xsi");
   tryListWithConfiguration(JSONConfiguration.mappedJettison().xml2JsonNs(ns2json).build());
   tryIndividualsWithConfiguration(JSONConfiguration.mappedJettison().xml2JsonNs(ns2json).build());
 }
  public static XMLStreamReader createReader(
      Reader reader, JSONConfiguration config, String rootName, boolean readingList)
      throws XMLStreamException {

    Reader nonEmptyReader = ensureNonEmptyReader(reader);

    switch (config.getNotation()) {
      case NATURAL:
        try {
          final JsonParser rawParser = new JsonFactory().createJsonParser(nonEmptyReader);
          final JsonParser nonListParser =
              config.isRootUnwrapping()
                  ? JacksonRootAddingParser.createRootAddingParser(rawParser, rootName)
                  : rawParser;
          if (!readingList) {
            return new Jackson2StaxReader(nonListParser, config);
          } else {
            return new Jackson2StaxReader(
                JacksonRootAddingParser.createRootAddingParser(
                    nonListParser, "jsonArrayRootElement"),
                config);
          }
        } catch (Exception ex) {
          throw new XMLStreamException(ex);
        }
      case MAPPED:
        try {
          return new JsonXmlStreamReader(nonEmptyReader, rootName, config);
        } catch (IOException ex) {
          throw new XMLStreamException(ex);
        }
      case MAPPED_JETTISON:
        try {
          Configuration jmConfig;
          if (null == config.getXml2JsonNs()) {
            jmConfig = new Configuration();
          } else {
            jmConfig = new Configuration(config.getXml2JsonNs());
          }
          return new MappedXMLStreamReader(
              new JSONObject(new JSONTokener(ReaderWriter.readFromAsString(nonEmptyReader))),
              new MappedNamespaceConvention(jmConfig));
        } catch (Exception ex) {
          throw new XMLStreamException(ex);
        }
      case BADGERFISH:
        try {
          return new BadgerFishXMLStreamReader(
              new JSONObject(new JSONTokener(ReaderWriter.readFromAsString(nonEmptyReader))));
        } catch (Exception ex) {
          throw new XMLStreamException(ex);
        }
    }
    // This should not occur
    throw new IllegalArgumentException("Unknown JSON config");
  }
예제 #3
0
  public JAXBContextResolver() throws Exception {

    JAXBContext context;
    JAXBContext unWrappedRootContext;

    // you have to specify all the dao classes here
    final Class[] cTypes = {
      AppInfo.class,
      AppAttemptInfo.class,
      AppAttemptsInfo.class,
      ClusterInfo.class,
      CapacitySchedulerQueueInfo.class,
      FifoSchedulerInfo.class,
      SchedulerTypeInfo.class,
      NodeInfo.class,
      UserMetricsInfo.class,
      CapacitySchedulerInfo.class,
      ClusterMetricsInfo.class,
      SchedulerInfo.class,
      AppsInfo.class,
      NodesInfo.class,
      RemoteExceptionData.class,
      CapacitySchedulerQueueInfoList.class,
      ResourceInfo.class,
      UsersInfo.class,
      UserInfo.class,
      ApplicationStatisticsInfo.class,
      StatisticsItemInfo.class,
      CapacitySchedulerHealthInfo.class,
      FairSchedulerQueueInfoList.class
    };
    // these dao classes need root unwrapping
    final Class[] rootUnwrappedTypes = {
      NewApplication.class,
      ApplicationSubmissionContextInfo.class,
      ContainerLaunchContextInfo.class,
      LocalResourceInfo.class,
      DelegationToken.class,
      AppQueue.class
    };

    this.typesContextMap = new HashMap<Class, JAXBContext>();
    context =
        new JSONJAXBContext(JSONConfiguration.natural().rootUnwrapping(false).build(), cTypes);
    unWrappedRootContext =
        new JSONJAXBContext(
            JSONConfiguration.natural().rootUnwrapping(true).build(), rootUnwrappedTypes);
    for (Class type : cTypes) {
      typesContextMap.put(type, context);
    }
    for (Class type : rootUnwrappedTypes) {
      typesContextMap.put(type, unWrappedRootContext);
    }
  }
  public JAXBContext getContext(Class<?> objectType) {
    if (types.contains(objectType)) {
      return context;
    }

    // need to see if class has any ArrayList types, if so, add those to arrays
    Field[] fields = objectType.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
      if (fields[i].getType().isAssignableFrom(ArrayList.class)) {
        String simpleName = fields[i].getName();
        simpleName = simpleName.substring(0, 1).toLowerCase() + simpleName.substring(1);
        arrays.add(simpleName);
      }
    }

    String simpleName = objectType.getSimpleName();
    simpleName = simpleName.substring(0, 1).toLowerCase() + simpleName.substring(1);
    arrays.add(simpleName);
    types.add(objectType);
    try {
      JSONConfiguration config =
          JSONConfiguration.mapped()
              .rootUnwrapping(true)
              .arrays(arrays.toArray(new String[] {}))
              .build();
      context = new JSONJAXBContext(config, types.toArray(new Class[] {}));
      return getContext(objectType);
    } catch (JAXBException e) {
    }
    return null;
  }
예제 #5
0
 /**
  * @param s the JSON representation of the filter
  * @return the filter
  * @throws Exception
  */
 public static Filter buildFilter(String s) throws Exception {
   JSONJAXBContext context =
       new JSONJAXBContext(JSONConfiguration.natural().build(), FilterModel.class);
   JSONUnmarshaller unmarshaller = context.createJSONUnmarshaller();
   FilterModel model = unmarshaller.unmarshalFromJSON(new StringReader(s), FilterModel.class);
   return model.build();
 }
예제 #6
0
 /**
  * @param filter the filter
  * @return the JSON representation of the filter
  * @throws Exception
  */
 public static String stringifyFilter(final Filter filter) throws Exception {
   JSONJAXBContext context =
       new JSONJAXBContext(JSONConfiguration.natural().build(), FilterModel.class);
   JSONMarshaller marshaller = context.createJSONMarshaller();
   StringWriter writer = new StringWriter();
   marshaller.marshallToJSON(new FilterModel(filter), writer);
   return writer.toString();
 }
예제 #7
0
 public void testNatural() throws Exception {
   System.out.println("\nTesting Natural: -------------------------");
   // TODO: a patch applied at jaxb trunk to add a new utility method on UnmarshallingContext
   //            after this gets tested and make it to a release of jaxb, we can uncomment
   // appropriate
   //            stuff on Jersey side, and the following should work
   // tryListWithConfiguration(JSONConfiguration.natural().build());
   tryIndividualsWithConfiguration(JSONConfiguration.natural().rootUnwrapping(false).build());
 }
 public JAXBContextResolver() throws Exception {
   types.add(ArrayList.class);
   types.add(JaxbList.class);
   arrays.add("list");
   arrays.add("values");
   JSONConfiguration config =
       JSONConfiguration.mapped()
           .rootUnwrapping(true)
           .arrays(arrays.toArray(new String[] {}))
           .build();
   context = new JSONJAXBContext(config, types.toArray(new Class[] {}));
 }
  public void testNewLineAdded() throws Exception {

    final JSONJAXBContext ctx =
        new JSONJAXBContext(
            JSONConfiguration.natural().rootUnwrapping(false).humanReadableFormatting(true).build(),
            User.class);
    final JSONMarshaller jm = ctx.createJSONMarshaller();
    final StringWriter sw = new StringWriter();

    final User one = JSONTestHelper.createTestInstance(User.class);
    jm.marshallToJSON(one, sw);

    assertTrue(sw.toString().contains("\n"));
  }
 public static XMLStreamWriter createWriter(
     Writer writer, JSONConfiguration config, boolean writingList) throws IOException {
   switch (config.getNotation()) {
     case NATURAL:
       final JsonGenerator rawGenerator = new JsonFactory().createJsonGenerator(writer);
       if (config.isHumanReadableFormatting()) {
         rawGenerator.useDefaultPrettyPrinter();
       }
       final JsonGenerator bodyGenerator =
           writingList
               ? JacksonArrayWrapperGenerator.createArrayWrapperGenerator(
                   rawGenerator, config.isRootUnwrapping() ? 0 : 1)
               : rawGenerator;
       if (config.isRootUnwrapping()) {
         return new Stax2JacksonWriter(
             JacksonRootStrippingGenerator.createRootStrippingGenerator(
                 bodyGenerator, writingList ? 2 : 1),
             config);
       } else {
         return new Stax2JacksonWriter(bodyGenerator, config);
       }
     case MAPPED:
       return JsonXmlStreamWriter.createWriter(writer, config);
     case BADGERFISH:
       return new BadgerFishXMLStreamWriter(writer);
     case MAPPED_JETTISON:
       Configuration jmConfig;
       if (null == config.getXml2JsonNs()) {
         jmConfig = new Configuration();
       } else {
         jmConfig = new Configuration(config.getXml2JsonNs());
       }
       return new MappedXMLStreamWriter(new MappedNamespaceConvention(jmConfig), writer);
     default:
       return null;
   }
 }
 public JAXBContextResolver() throws Exception {
   this.context = new JSONJAXBContext(JSONConfiguration.natural().build(), types);
 }
예제 #12
0
 public void testBadgerfish() throws Exception {
   System.out.println("\nTesting BadgerFish: ------------------------");
   tryListWithConfiguration(JSONConfiguration.badgerFish().build());
   tryIndividualsWithConfiguration(JSONConfiguration.badgerFish().build());
 }