List of usage examples for com.fasterxml.jackson.core JsonGenerator writeEndObject
public abstract void writeEndObject() throws IOException, JsonGenerationException;
From source file:com.msopentech.odatajclient.engine.data.json.DOMTreeUtilsV3.java
/** * Serializes DOM content as JSON.// w w w.j a va 2 s . c om * * @param client OData client. * @param jgen JSON generator. * @param content content. * @param propType whether to output type information in the way needed for property values or not. * @throws IOException in case of write error. */ public static void writeSubtree(final ODataClient client, final JsonGenerator jgen, final Node content, final boolean propType) throws IOException { for (Node child : XMLUtils.getChildNodes(content, Node.ELEMENT_NODE)) { final String childName = XMLUtils.getSimpleName(child); final Node typeAttr = child.getAttributes().getNamedItem(ODataConstants.ATTR_M_TYPE); if (typeAttr != null && EdmSimpleType.isGeospatial(typeAttr.getTextContent())) { jgen.writeStringField( propType ? ODataConstants.JSON_TYPE : childName + "@" + ODataConstants.JSON_TYPE, typeAttr.getTextContent()); jgen.writeObjectFieldStart(childName); GeospatialJSONHandler.serialize(jgen, (Element) child, typeAttr.getTextContent()); jgen.writeEndObject(); } else if (XMLUtils.hasOnlyTextChildNodes(child)) { if (child.hasChildNodes()) { final String out; if (typeAttr == null) { out = child.getChildNodes().item(0).getNodeValue(); } else { final EdmSimpleType type = EdmSimpleType.fromValue(typeAttr.getTextContent()); final ODataPrimitiveValue value = client.getPrimitiveValueBuilder().setType(type) .setText(child.getChildNodes().item(0).getNodeValue()).build(); out = value.toString(); jgen.writeStringField(childName + "@" + ODataConstants.JSON_TYPE, type.toString()); } jgen.writeStringField(childName, out); } else { if (child.getAttributes().getNamedItem(ODataConstants.ATTR_NULL) == null) { if (typeAttr != null && EdmSimpleType.String.toString().equals(typeAttr.getTextContent())) { jgen.writeStringField(childName + "@" + ODataConstants.JSON_TYPE, typeAttr.getTextContent()); jgen.writeStringField(childName, StringUtils.EMPTY); } else { jgen.writeArrayFieldStart(childName); jgen.writeEndArray(); } } else { jgen.writeNullField(childName); } } } else { if (XMLUtils.hasElementsChildNode(child)) { jgen.writeArrayFieldStart(childName); for (Node nephew : XMLUtils.getChildNodes(child, Node.ELEMENT_NODE)) { if (XMLUtils.hasOnlyTextChildNodes(nephew)) { jgen.writeString(nephew.getChildNodes().item(0).getNodeValue()); } else { jgen.writeStartObject(); DOMTreeUtils.writeSubtree(client, jgen, nephew); jgen.writeEndObject(); } } jgen.writeEndArray(); } else { jgen.writeObjectFieldStart(childName); if (typeAttr != null) { jgen.writeStringField(ODataConstants.JSON_TYPE, typeAttr.getTextContent()); } DOMTreeUtils.writeSubtree(client, jgen, child); jgen.writeEndObject(); } } } }
From source file:com.meetingninja.csse.database.UserDatabaseAdapter.java
public static String login(String email, String pass) throws IOException { // Server URL setup String _url = getBaseUri().appendPath("Login").build().toString(); // Establish connection URL url = new URL(_url); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // add request header conn.setRequestMethod(IRequest.POST); addRequestHeader(conn, true);/*from w w w. j av a 2 s . co m*/ // prepare POST payload ByteArrayOutputStream json = new ByteArrayOutputStream(); // this type of print stream allows us to get a string easily PrintStream ps = new PrintStream(json); // Create a generator to build the JSON string JsonGenerator jgen = JFACTORY.createGenerator(ps, JsonEncoding.UTF8); try { // hash the password pass = Utilities.computeHash(pass); } catch (NoSuchAlgorithmException e) { Log.e(TAG, e.getLocalizedMessage()); } // Build JSON Object jgen.writeStartObject(); jgen.writeStringField(Keys.User.EMAIL, email); jgen.writeStringField("password", pass); jgen.writeEndObject(); jgen.close(); // Get JSON Object payload from print stream String payload = json.toString("UTF8"); ps.close(); // Send payload int responseCode = sendPostPayload(conn, payload); String response = getServerResponse(conn); /* * result should get valid={"userID":"##"} * invalid={"errorID":"3","errorMessage":"invalid username or password"} */ String result = ""; if (!response.isEmpty()) { JsonNode tree = MAPPER.readTree(response); if (!tree.has(Keys.User.ID)) { logError(TAG, tree); result = "invalid username or password"; } else result = tree.get(Keys.User.ID).asText(); } conn.disconnect(); return result; }
From source file:com.meetingninja.csse.database.MeetingDatabaseAdapter.java
public static Meeting createMeeting(String userID, Meeting m) throws IOException, MalformedURLException { // Server URL setup String _url = getBaseUri().appendPath(userID).build().toString(); // establish connection URL url = new URL(_url); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); addRequestHeader(conn, true);//from ww w .ja v a 2s . c o m // prepare POST payload ByteArrayOutputStream json = new ByteArrayOutputStream(); // this type of print stream allows us to get a string easily PrintStream ps = new PrintStream(json); // Create a generator to build the JSON string JsonGenerator jgen = JFACTORY.createGenerator(ps, JsonEncoding.UTF8); // Build JSON Object jgen.writeStartObject(); jgen.writeStringField(Keys.User.ID, userID); jgen.writeStringField(Keys.Meeting.TITLE, m.getTitle()); jgen.writeStringField(Keys.Meeting.LOCATION, m.getLocation()); jgen.writeStringField(Keys.Meeting.DATETIME, m.getStartTime()); jgen.writeStringField(Keys.Meeting.OTHEREND, m.getEndTime()); jgen.writeStringField(Keys.Meeting.DESC, m.getDescription()); jgen.writeArrayFieldStart(Keys.Meeting.ATTEND); // TODO: Add attendees to meeting // for (String attendee : m.getAttendance()) { // if (attendee.isAttending()) { jgen.writeStartObject(); jgen.writeStringField(Keys.User.ID, userID); jgen.writeEndObject(); // } // } jgen.writeEndArray(); jgen.writeEndObject(); jgen.close(); // Get JSON Object payload from print stream String payload = json.toString("UTF8"); ps.close(); // send payload int responseCode = sendPostPayload(conn, payload); String response = getServerResponse(conn); // prepare to get the id of the created Meeting JsonNode responseMap; m.setID(404); Meeting created = new Meeting(m); /* * result should get valid={"meetingID":"##"} */ // String result = new String(); if (!response.isEmpty()) { responseMap = MAPPER.readTree(response); if (responseMap.has(Keys.Meeting.ID)) created.setID(responseMap.get(Keys.Meeting.ID).asText()); } // if (!result.equalsIgnoreCase("invalid")) // created.setID(result); conn.disconnect(); return created; }
From source file:com.msopentech.odatajclient.engine.data.json.DOMTreeUtils.java
/** * Serializes DOM content as JSON./*from w w w.ja v a2 s . c o m*/ * * @param jgen JSON generator. * @param content content. * @param propType whether to output type information in the way needed for property values or not. * @throws IOException in case of write error. */ public static void writeSubtree(final JsonGenerator jgen, final Node content, final boolean propType) throws IOException { for (Node child : XMLUtils.getChildNodes(content, Node.ELEMENT_NODE)) { final String childName = XMLUtils.getSimpleName(child); final Node typeAttr = child.getAttributes().getNamedItem(ODataConstants.ATTR_M_TYPE); if (typeAttr != null && EdmSimpleType.isGeospatial(typeAttr.getTextContent())) { jgen.writeStringField( propType ? ODataConstants.JSON_TYPE : childName + "@" + ODataConstants.JSON_TYPE, typeAttr.getTextContent()); jgen.writeObjectFieldStart(childName); GeospatialJSONHandler.serialize(jgen, (Element) child, typeAttr.getTextContent()); jgen.writeEndObject(); } else if (XMLUtils.hasOnlyTextChildNodes(child)) { if (child.hasChildNodes()) { final String out; if (typeAttr == null) { out = child.getChildNodes().item(0).getNodeValue(); } else { final EdmSimpleType type = EdmSimpleType.fromValue(typeAttr.getTextContent()); final ODataPrimitiveValue value = new ODataPrimitiveValue.Builder().setType(type) .setText(child.getChildNodes().item(0).getNodeValue()).build(); out = value.toString(); jgen.writeStringField(childName + "@" + ODataConstants.JSON_TYPE, type.toString()); } jgen.writeStringField(childName, out); } else { if (child.getAttributes().getNamedItem(ODataConstants.ATTR_NULL) == null) { if (typeAttr != null && EdmSimpleType.String.toString().equals(typeAttr.getTextContent())) { jgen.writeStringField(childName + "@" + ODataConstants.JSON_TYPE, typeAttr.getTextContent()); jgen.writeStringField(childName, StringUtils.EMPTY); } else { jgen.writeArrayFieldStart(childName); jgen.writeEndArray(); } } else { jgen.writeNullField(childName); } } } else { if (XMLUtils.hasElementsChildNode(child)) { jgen.writeArrayFieldStart(childName); for (Node nephew : XMLUtils.getChildNodes(child, Node.ELEMENT_NODE)) { if (XMLUtils.hasOnlyTextChildNodes(nephew)) { jgen.writeString(nephew.getChildNodes().item(0).getNodeValue()); } else { jgen.writeStartObject(); writeSubtree(jgen, nephew); jgen.writeEndObject(); } } jgen.writeEndArray(); } else { jgen.writeObjectFieldStart(childName); if (typeAttr != null) { jgen.writeStringField(ODataConstants.JSON_TYPE, typeAttr.getTextContent()); } writeSubtree(jgen, child); jgen.writeEndObject(); } } } }
From source file:com.attribyte.essem.ESReporter.java
static final void generateCounter(final JsonGenerator generator, final ReportProtos.EssemReport.Counter counter, final String application, final String host, final String instance, final long timestamp) throws IOException { generator.writeStartObject();//w ww. j ava 2 s. c o m writeStringField(generator, Fields.APPLICATION_FIELD, application); writeStringField(generator, Fields.HOST_FIELD, host); writeStringField(generator, Fields.INSTANCE_FIELD, instance); writeStringField(generator, Fields.NAME_FIELD, counter.getName().trim()); generator.writeNumberField(Fields.COUNT_FIELD, counter.getCount()); generator.writeNumberField(Fields.TIMESTAMP_FIELD, timestamp); generator.writeEndObject(); generator.flush(); }
From source file:com.msopentech.odatajclient.engine.data.impl.JSONDOMTreeUtils.java
/** * Serializes DOM content as JSON.//from www .ja v a 2 s .c om * * @param client OData client. * @param jgen JSON generator. * @param content content. * @param propType whether to output type information in the way needed for property values or not. * @throws IOException in case of write error. */ public static void writeSubtree(final ODataClient client, final JsonGenerator jgen, final Node content, final boolean propType) throws IOException { for (Node child : XMLUtils.getChildNodes(content, Node.ELEMENT_NODE)) { final String childName = XMLUtils.getSimpleName(child); final Node typeAttr = child.getAttributes().getNamedItem(ODataConstants.ATTR_M_TYPE); if (typeAttr != null && EdmSimpleType.isGeospatial(typeAttr.getTextContent())) { jgen.writeStringField( propType ? ODataConstants.JSON_TYPE : childName + "@" + ODataConstants.JSON_TYPE, typeAttr.getTextContent()); jgen.writeObjectFieldStart(childName); GeospatialJSONHandler.serialize(client, jgen, (Element) child, typeAttr.getTextContent()); jgen.writeEndObject(); } else if (XMLUtils.hasOnlyTextChildNodes(child)) { if (child.hasChildNodes()) { final String out; if (typeAttr == null) { out = child.getChildNodes().item(0).getNodeValue(); } else { final EdmSimpleType type = EdmSimpleType.fromValue(typeAttr.getTextContent()); final ODataPrimitiveValue value = client.getPrimitiveValueBuilder().setType(type) .setText(child.getChildNodes().item(0).getNodeValue()).build(); out = value.toString(); jgen.writeStringField(childName + "@" + ODataConstants.JSON_TYPE, type.toString()); } jgen.writeStringField(childName, out); } else { if (child.getAttributes().getNamedItem(ODataConstants.ATTR_NULL) == null) { if (typeAttr != null && EdmSimpleType.String.toString().equals(typeAttr.getTextContent())) { jgen.writeStringField(childName + "@" + ODataConstants.JSON_TYPE, typeAttr.getTextContent()); jgen.writeStringField(childName, StringUtils.EMPTY); } else { jgen.writeArrayFieldStart(childName); jgen.writeEndArray(); } } else { jgen.writeNullField(childName); } } } else { if (XMLUtils.hasElementsChildNode(child)) { jgen.writeArrayFieldStart(childName); for (Node nephew : XMLUtils.getChildNodes(child, Node.ELEMENT_NODE)) { if (XMLUtils.hasOnlyTextChildNodes(nephew)) { jgen.writeString(nephew.getChildNodes().item(0).getNodeValue()); } else { jgen.writeStartObject(); writeSubtree(client, jgen, nephew); jgen.writeEndObject(); } } jgen.writeEndArray(); } else { jgen.writeObjectFieldStart(childName); if (typeAttr != null) { jgen.writeStringField(ODataConstants.JSON_TYPE, typeAttr.getTextContent()); } writeSubtree(client, jgen, child); jgen.writeEndObject(); } } } }
From source file:org.bndtools.rt.repository.marshall.CapReqJson.java
public static void writeCapability(Capability capability, JsonGenerator generator) throws IOException { generator.writeStartObject();/* ww w . j ava2s .c om*/ generator.writeStringField("ns", capability.getNamespace()); Map<String, Object> attrs = capability.getAttributes(); if (!attrs.isEmpty()) { generator.writeArrayFieldStart("attrs"); for (Entry<String, Object> entry : attrs.entrySet()) { writeAttribute(entry.getKey(), entry.getValue(), generator); } generator.writeEndArray(); } Map<String, String> dirs = capability.getDirectives(); if (!dirs.isEmpty()) { generator.writeArrayFieldStart("dirs"); for (Entry<String, String> entry : dirs.entrySet()) { writeDirective(entry.getKey(), entry.getValue(), generator); } generator.writeEndArray(); } generator.writeEndObject(); }
From source file:net.echinopsii.ariane.community.core.directory.wat.json.ds.organisational.ApplicationJSON.java
public final static void application2JSON(Application application, JsonGenerator jgenerator) throws IOException { jgenerator.writeStartObject();//from w ww .j a v a2 s . c o m jgenerator.writeNumberField(APP_ID, application.getId()); jgenerator.writeNumberField(APP_VERSION, application.getVersion()); jgenerator.writeStringField(APP_NAME, application.getName()); jgenerator.writeStringField(APP_SHORT_NAME, application.getShortName()); jgenerator.writeStringField(APP_COLOR_CODE, application.getColorCode()); jgenerator.writeStringField(APP_DESCRIPTION, application.getDescription()); jgenerator.writeArrayFieldStart(APP_OSI_ID); for (OSInstance osi : application.getOsInstances()) jgenerator.writeNumber(osi.getId()); jgenerator.writeEndArray(); jgenerator.writeNumberField(APP_TEAM_ID, ((application.getTeam() != null) ? application.getTeam().getId() : -1)); jgenerator.writeNumberField(APP_COMPANY_ID, ((application.getCompany() != null) ? application.getCompany().getId() : -1)); jgenerator.writeEndObject(); }
From source file:org.bndtools.rt.repository.marshall.CapReqJson.java
public static void writeRequirement(Requirement requirement, JsonGenerator generator) throws IOException { generator.writeStartObject();/*from w w w . j a v a 2 s . co m*/ generator.writeStringField("ns", requirement.getNamespace()); Map<String, Object> attrs = requirement.getAttributes(); if (!attrs.isEmpty()) { generator.writeArrayFieldStart("attrs"); for (Entry<String, Object> entry : attrs.entrySet()) { writeAttribute(entry.getKey(), entry.getValue(), generator); } generator.writeEndArray(); } Map<String, String> dirs = requirement.getDirectives(); if (!dirs.isEmpty()) { generator.writeArrayFieldStart("dirs"); for (Entry<String, String> entry : dirs.entrySet()) { writeDirective(entry.getKey(), entry.getValue(), generator); } generator.writeEndArray(); } generator.writeEndObject(); }
From source file:de.alexkamp.sandbox.model.SpecialMount.java
@Override public void toJSON(JsonGenerator gen) throws IOException { gen.writeStartObject();// w w w.j a va 2s.c om gen.writeObjectField("type", type); gen.writeEndObject(); }