Package net.arnx.jsonic

Examples of net.arnx.jsonic.JSON$Context


        m.setLongAttr(10000);
        m.setFloatAttr(1.1f);
        m.setDoubleAttr(11.1);
        String json = PrimitiveAttrsModelMeta.get().modelToJson(m);
        System.out.println(json);
        JSON j = new JSON();
        j.setSuppressNull(true);
        System.out.println(j.format(m));
        Assert.assertEquals("{\"booleanAttr\":true,\"doubleAttr\":11.1"
            + ",\"floatAttr\":1.1,\"intAttr\":1000"
            + ",\"longAttr\":10000,\"shortAttr\":100}", json);
    }
View Full Code Here


            new User("email", "authDomain", "userId"),
            new User("email", "authDomain", "userId", "federatedId")));

        String json = AppEngineTypeListAttrsModelMeta.get().modelToJson(m);
        System.out.println(json);
        JSON j = new JSON();
        j.setSuppressNull(true);
        System.out.println(j.format(m));
        // JSON.decode(json);

        Assert
            .assertEquals(
                "{\"blobKeyListAttr\":[\"lkwejl2k3jrksl\",\"kaekl23joij\"]"
View Full Code Here

        model.setLongListAttr(Arrays.asList(9L, 8L, 7L));
        model.setFloatListAttr(Arrays.asList(9.9f, 8.8f, 7.7f));
        model.setDoubleListAttr(Arrays.asList(9.9, 8.8, 7.7));
        String json = m.modelToJson(model);
        System.out.println(json);
        JSON j = new JSON();
        j.setSuppressNull(true);
        System.out.println(j.format(model));
        Assert
            .assertEquals(
                "{\"booleanListAttr\":[true,false,true],\"doubleListAttr\":[9.9,8.8,7.7],\"floatListAttr\":[9.9,8.8,7.7]"
                    + ",\"integerListAttr\":[9,8,7],\"longListAttr\":[9,8,7]"
                    + ",\"shortListAttr\":[9,8,7]}",
View Full Code Here

            "userId",
            "federatedId"));

        String json = AppEngineTypeAttrsModelMeta.get().modelToJson(m);
        System.out.println(json);
        JSON j = new JSON();
        j.setSuppressNull(true);
        System.out.println(j.format(m));

        assertEquals(
            "{"
                + "\"blobAttr\":\"aGVsbG8=\""
                + ",\"blobKeyAttr\":\"Q3PqkweYlb4iWpp0BVw\",\"categoryAttr\":\"partOfSpeech\""
View Full Code Here

        Element requirements = results.getChild("requirements");

        assertEquals(SchematronRequirement.values().length, requirements.getChildren().size());

        String json = Xml.getJSON(schemas);
        new JSON().parse(json);
        // no exception ??? good

        assertEqualsText("iso19139", schemas, "iso19139/name");
        String resultAsString = Xml.getString(schemas);
View Full Code Here

  public void init(ServletConfig servletConfig) throws ServletException {
    super.init(servletConfig);
   
    String configText = servletConfig.getInitParameter("config");
   
    JSON json = new JSON();
   
    if (configText == null) {
      Map<String, String> map = new HashMap<String, String>();
      Enumeration<String> e =  cast(servletConfig.getInitParameterNames());
      while (e.hasMoreElements()) {
        map.put(e.nextElement(), servletConfig.getInitParameter(e.nextElement()));
      }
      configText = json.format(map);
    }
   
    try {
      config = json.parse(configText, Config.class);
      if (config.container == null) config.container = Container.class;
      container = json.parse(configText, config.container);
      container.init(this);
    } catch (Exception e) {
      throw new ServletException(e);
    }
   
View Full Code Here

      status = SC_CREATED;
    }
   
    container.start(request, response);
   
    JSON json = container.createJSON(request.getLocale());
   
    try {
      String className = route.getComponentClass(container);
      Object component = container.getComponent(className);
      if (component == null) {
        throw new ClassNotFoundException("Component not found: " + className);
      }
     
      List<Object> params = null;
      if (isJSONType(request.getContentType())) {
        Object o = json.parse(request.getReader());
        if (o instanceof List<?>) {
          params = cast(o);
          if (params.isEmpty()) {
            params = new ArrayList<Object>(1);
            params.add(route.getParameterMap());
          } else if (params.get(0) instanceof Map<?, ?>) {
            params.set(0, route.mergeParameterMap((Map<?, ?>)params.get(0)));
          }
        } else if (o instanceof Map<?, ?>) {
          params = new ArrayList<Object>(1);
          params.add(route.mergeParameterMap((Map<?, ?>)o));
        } else {
          throw new IllegalArgumentException("failed to convert parameters from JSON.");
        }
      } else {
        params = new ArrayList<Object>(1);
        params.add(route.getParameterMap());       
      }
     
      Method method = container.getMethod(component, route.getRestMethod(), params);
      if (method == null) {
        throw new NoSuchMethodException("Method not found: " + route.getRestMethod());         
      }
     
      json.setContext(component);
      result = container.execute(json, component, method, params);
    } catch (Exception e) {
      if (e instanceof ClassNotFoundException) {
        container.debug("Class Not Found.", e);
        response.sendError(SC_NOT_FOUND, "Not Found");
        response.flushBuffer();
      } else if (e instanceof NoSuchMethodException) {
        container.debug("Method Not Found.", e);
        response.sendError(SC_NOT_FOUND, "Not Found");
        response.flushBuffer();
      } else if (e instanceof JSONException) {
        container.debug("Fails to parse JSON.", e);
        response.sendError(SC_BAD_REQUEST, "Bad Request");
        response.flushBuffer();
      } else if (e instanceof InvocationTargetException) {
        Throwable cause = e.getCause();
        container.debug("Cause error on invocation.", cause);
        if (cause instanceof Error) {
          throw (Error)cause;
        } else if (cause instanceof IllegalStateException || cause instanceof UnsupportedOperationException) {
          response.sendError(SC_NOT_FOUND, "Not Found");
          response.flushBuffer();
        } else if (cause instanceof IllegalArgumentException) {
          response.sendError(SC_BAD_REQUEST, "Bad Request");
          response.flushBuffer();
        } else {
          Integer errorCode = null;
          for (Map.Entry<Class<? extends Exception>, Integer> entry : config.errors.entrySet()) {
            if (entry.getKey().isAssignableFrom(cause.getClass()) && entry.getValue() != null) {
              errorCode = entry.getValue();
              break;
            }
          }
          if (errorCode != null) {
            response.setStatus(errorCode);
           
            Map<String, Object> error = new LinkedHashMap<String, Object>();
            error.put("name", cause.getClass().getSimpleName());
            error.put("message", cause.getMessage());
            error.put("data", cause);
            result = error;
          } else {
            response.sendError(SC_INTERNAL_SERVER_ERROR, "Internal Server Error");       
            response.flushBuffer();
          }
        }
      } else {
        container.error("Internal error occurred.", e);
        response.sendError(SC_INTERNAL_SERVER_ERROR, "Internal Server Error");       
        response.flushBuffer();
      }
    } finally {
      container.end(request, response);
    }
   
    if (response.isCommitted()) return;
   
    if (result == null
        || result instanceof CharSequence
        || result instanceof Boolean
        || result instanceof Number
        || result instanceof Date) {
      if (status != SC_CREATED) status = SC_NO_CONTENT;
      response.setStatus(status);
    } else {
      response.setContentType((callback != null) ? "text/javascript" : "application/json");
      Writer writer = response.getWriter();
      json.setPrettyPrint(container.isDebugMode());
     
      if (callback != null) writer.append(callback).append("(");
      json.format(result, writer);
      if (callback != null) writer.append(");");
    }
  }
View Full Code Here

  }
 
  public void init(FilterConfig filterConfig) throws ServletException {
    this.context = filterConfig.getServletContext();
   
    JSON json = new JSON();
    json.setContext(this);

    String configText = filterConfig.getInitParameter("config");
    if (configText == null) configText = "";

    Map map = json.parse(configText, Map.class);
   
    Map<String, Object> baseMap = new LinkedHashMap<String, Object>();
    for (Field field : Config.class.getFields()) {
      baseMap.put(field.getName(), map.get(field.getName()));
    }
   
    Config base = (Config)json.convert(baseMap, Config.class);
    for (Map.Entry entry : (Set<Map.Entry>)map.entrySet()) {
      if (!baseMap.containsKey(entry.getKey()) && entry.getValue() instanceof Map) {
        Map valueMap = (Map)entry.getValue();
        for (Map.Entry<String, Object> baseEntry : baseMap.entrySet()) {
          if (valueMap.get(baseEntry.getKey()) == null) {
            valueMap.put(baseEntry.getKey(), baseEntry.getValue());
          }
        }
       
        Config config = (Config)json.convert(valueMap, Config.class);
        locations.put(Pattern.compile("^" + entry.getKey() + "$"), config);
      }
    }
    locations.put(Pattern.compile(".*"), base);
  }
View Full Code Here

  }
 
  public void init(FilterConfig filterConfig) throws ServletException {
    this.context = filterConfig.getServletContext();
   
    JSON json = new JSON();
    json.setContext(this);

    String configText = filterConfig.getInitParameter("config");
    if (configText == null) configText = "";

    Map map = json.parse(configText, Map.class);
   
    Map<String, Object> baseMap = new LinkedHashMap<String, Object>();
    for (Field field : Config.class.getFields()) {
      baseMap.put(field.getName(), map.get(field.getName()));
    }
   
    Config base = (Config)json.convert(baseMap, Config.class);
    for (Map.Entry entry : (Set<Map.Entry>)map.entrySet()) {
      if (!baseMap.containsKey(entry.getKey()) && entry.getValue() instanceof Map) {
        Map valueMap = (Map)entry.getValue();
        for (Map.Entry<String, Object> baseEntry : baseMap.entrySet()) {
          if (valueMap.get(baseEntry.getKey()) == null) {
            valueMap.put(baseEntry.getKey(), baseEntry.getValue());
          }
        }
       
        Config config = (Config)json.convert(valueMap, Config.class);
        locations.put(Pattern.compile("^" + entry.getKey() + "$"), config);
      }
    }
    locations.put(Pattern.compile(".*"), base);
  }
View Full Code Here

  public void init(ServletConfig servletConfig) throws ServletException {
    super.init(servletConfig);
   
    String configText = servletConfig.getInitParameter("config");
   
    JSON json = new JSON();
   
    if (configText == null) {
      Map<String, String> map = new HashMap<String, String>();
      Enumeration<String> e =  cast(servletConfig.getInitParameterNames());
      while (e.hasMoreElements()) {
        map.put(e.nextElement(), servletConfig.getInitParameter(e.nextElement()));
      }
      configText = json.format(map);
    }
   
    try {
      config = json.parse(configText, Config.class);
      if (config.container == null) config.container = Container.class;
      container = json.parse(configText, config.container);
      container.init(this);
    } catch (Exception e) {
      throw new ServletException(e);
    }
   
View Full Code Here

TOP

Related Classes of net.arnx.jsonic.JSON$Context

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.