List of usage examples for javax.json JsonWriter close
@Override
void close();
From source file:csg.files.CSGFiles.java
public void saveScheduleData(AppDataComponent schData, String filePath) throws IOException { ScheduleData schDataManager = (ScheduleData) schData; JsonArrayBuilder schArrayBuilder = Json.createArrayBuilder(); ObservableList<ScheduleItem> schedule = schDataManager.getSchedule(); ArrayList<ScheduleItem> holidays = new ArrayList(); ArrayList<ScheduleItem> lectures = new ArrayList(); ArrayList<ScheduleItem> references = new ArrayList(); ArrayList<ScheduleItem> recitations = new ArrayList(); ArrayList<ScheduleItem> hws = new ArrayList(); for (int i = 0; i < schedule.size(); i++) { if (schedule.get(i).getType().equals("holiday")) { holidays.add(schedule.get(i)); } else if (schedule.get(i).getType().equals("lecture")) { lectures.add(schedule.get(i)); } else if (schedule.get(i).getType().equals("reference")) { references.add(schedule.get(i)); } else if (schedule.get(i).getType().equals("recitation")) { recitations.add(schedule.get(i)); } else if (schedule.get(i).getType().equals("hw")) { hws.add(schedule.get(i));// w w w . ja v a 2 s .com } } JsonArrayBuilder holidayArrayBuilder = Json.createArrayBuilder(); for (ScheduleItem scheduleItem : holidays) { JsonObject holidayJson = Json.createObjectBuilder().add(JSON_MONTH, scheduleItem.getDateMon()) .add(JSON_DAY, scheduleItem.getDateDay()).add(JSON_TITLE, scheduleItem.getTitle()) .add(JSON_LINK, scheduleItem.getLink()).build(); holidayArrayBuilder.add(holidayJson); } JsonArray holidayArray = holidayArrayBuilder.build(); JsonArrayBuilder lectureArrayBuilder = Json.createArrayBuilder(); for (ScheduleItem scheduleItem : lectures) { JsonObject lecJson = Json.createObjectBuilder().add(JSON_MONTH, scheduleItem.getDateMon()) .add(JSON_DAY, scheduleItem.getDateDay()).add(JSON_TITLE, scheduleItem.getTitle()) .add(JSON_TOPIC, scheduleItem.getTopic()).add(JSON_LINK, scheduleItem.getLink()).build(); lectureArrayBuilder.add(lecJson); } JsonArray lectureArray = lectureArrayBuilder.build(); JsonArrayBuilder referencesArrayBuilder = Json.createArrayBuilder(); for (ScheduleItem scheduleItem : references) { JsonObject lecJson = Json.createObjectBuilder().add(JSON_MONTH, scheduleItem.getDateMon()) .add(JSON_DAY, scheduleItem.getDateDay()).add(JSON_TITLE, scheduleItem.getTitle()) .add(JSON_TOPIC, scheduleItem.getTopic()).add(JSON_LINK, scheduleItem.getLink()).build(); referencesArrayBuilder.add(lecJson); } JsonArray referenceArray = referencesArrayBuilder.build(); JsonArrayBuilder recitationArrayBuilder = Json.createArrayBuilder(); for (ScheduleItem scheduleItem : recitations) { JsonObject lecJson = Json.createObjectBuilder().add(JSON_MONTH, scheduleItem.getDateMon()) .add(JSON_DAY, scheduleItem.getDateDay()).add(JSON_TITLE, scheduleItem.getTitle()) .add(JSON_TOPIC, scheduleItem.getTopic()).build(); recitationArrayBuilder.add(lecJson); } JsonArray recitaitonArray = recitationArrayBuilder.build(); JsonArrayBuilder hwArrayBuilder = Json.createArrayBuilder(); for (ScheduleItem scheduleItem : hws) { JsonObject lecJson = Json.createObjectBuilder().add(JSON_MONTH, scheduleItem.getDateMon()) .add(JSON_DAY, scheduleItem.getDateDay()).add(JSON_TITLE, scheduleItem.getTitle()) .add(JSON_TOPIC, scheduleItem.getTopic()).add(JSON_LINK, scheduleItem.getLink()) .add(JSON_TIME, scheduleItem.getTime()).add(JSON_CRITERIA, scheduleItem.getCriteria()).build(); hwArrayBuilder.add(lecJson); } JsonArray hwArray = hwArrayBuilder.build(); JsonObject dataManagerJSO = Json.createObjectBuilder().add(JSON_MONDAYMON, workspace.getMonMonthDate()) .add(JSON_MONDAYDAY, workspace.getMonDayDate()).add(JSON_FRIDAYMONTH, workspace.getFriMonthDate()) .add(JSON_FRIDAYDAY, workspace.getFriDayDate()).add(JSON_HOLIDAYS, holidayArray) .add(JSON_LECTURES, lectureArray).add(JSON_REF, referenceArray) .add(JSON_RECITATION, recitaitonArray).add(JSON_HW, hwArray).build(); // AND NOW OUTPUT IT TO A JSON FILE WITH PRETTY PRINTING Map<String, Object> properties = new HashMap<>(1); properties.put(JsonGenerator.PRETTY_PRINTING, true); JsonWriterFactory writerFactory = Json.createWriterFactory(properties); StringWriter sw = new StringWriter(); JsonWriter jsonWriter = writerFactory.createWriter(sw); jsonWriter.writeObject(dataManagerJSO); jsonWriter.close(); // INIT THE WRITER OutputStream os = new FileOutputStream(filePath); JsonWriter jsonFileWriter = Json.createWriter(os); jsonFileWriter.writeObject(dataManagerJSO); String prettyPrinted = sw.toString(); PrintWriter pw = new PrintWriter(filePath); pw.write(prettyPrinted); pw.close(); }
From source file:csg.files.CSGFiles.java
@Override public void saveData(AppDataComponent data, AppDataComponent recData, AppDataComponent schData, AppDataComponent projectData, AppDataComponent courseData, String filePath) throws IOException { // GET THE DATA TAData dataManager = (TAData) data;//w ww .j a va 2s . com CourseData cd = (CourseData) courseData; CSGWorkspace workspace = (CSGWorkspace) app.getWorkspaceComponent(); if (workspace.getTitleTextField().getText().equals("")) { cd.setTitle(""); } else { cd.setTitle(workspace.getTitleTextField().getText()); } if (workspace.getInsNameTextField().getText().equals("")) { cd.setInsName(""); } else { cd.setInsName(workspace.getInsNameTextField().getText()); } if (workspace.getInsHomeTextField().getText().equals("")) { cd.setInsHome(""); } else { cd.setInsHome(workspace.getInsHomeTextField().getText()); } // NOW BUILD THE TA JSON OBJCTS TO SAVE JsonArrayBuilder taArrayBuilder = Json.createArrayBuilder(); ObservableList<TeachingAssistant> tas = dataManager.getTeachingAssistants(); for (TeachingAssistant ta : tas) { JsonObject taJson = Json.createObjectBuilder().add(JSON_NAME, ta.getName()) .add(JSON_EMAIL, ta.getEmail()).add(JSON_UG, ta.isUndergrad().get()).build(); taArrayBuilder.add(taJson); } JsonArray undergradTAsArray = taArrayBuilder.build(); // NOW BUILD THE TIME SLOT JSON OBJCTS TO SAVE JsonArrayBuilder timeSlotArrayBuilder = Json.createArrayBuilder(); ArrayList<TimeSlot> officeHours = TimeSlot.buildOfficeHoursList(dataManager); for (TimeSlot ts : officeHours) { JsonObject tsJson = Json.createObjectBuilder().add(JSON_DAY, ts.getDay()).add(JSON_TIME, ts.getTime()) .add(JSON_NAME, ts.getName()).build(); timeSlotArrayBuilder.add(tsJson); } JsonArray timeSlotsArray = timeSlotArrayBuilder.build(); RecitationData recDataManager = (RecitationData) recData; JsonArrayBuilder recArrayBuilder = Json.createArrayBuilder(); ObservableList<Recitation> recitations = recDataManager.getRecitations(); for (Recitation recitation : recitations) { JsonObject recitationJson = Json.createObjectBuilder().add(JSON_SECTION, recitation.getSection()) .add(JSON_INSTRUCTOR, recitation.getInstructor()).add(JSON_DAYTIME, recitation.getDayTime()) .add(JSON_LOCATION, recitation.getLocation()).add(JSON_FIRSTTA, recitation.getFirstTA()) .add(JSON_SECONDTA, recitation.getSecondTA()).build(); recArrayBuilder.add(recitationJson); } JsonArray recitaitonArray = recArrayBuilder.build(); ScheduleData schDataManager = (ScheduleData) schData; JsonArrayBuilder schArrayBuilder = Json.createArrayBuilder(); ObservableList<ScheduleItem> schedule = schDataManager.getSchedule(); for (ScheduleItem scheduleItem : schedule) { JsonObject scheduleJson = Json.createObjectBuilder().add(JSON_TYPE, scheduleItem.getType()) .add(JSON_DAY, scheduleItem.getDate().getDayOfMonth()) .add(JSON_MONTH, scheduleItem.getDate().getMonthValue()) .add(JSON_YEAR, scheduleItem.getDate().getYear()).add(JSON_TIME, scheduleItem.getTime()) .add(JSON_TITLE, scheduleItem.getTitle()).add(JSON_TOPIC, scheduleItem.getTopic()) .add(JSON_LINK, scheduleItem.getLink()).add(JSON_CRITERIA, scheduleItem.getCriteria()).build(); schArrayBuilder.add(scheduleJson); } JsonArray scheduleArray = schArrayBuilder.build(); ProjectData projectDataManager = (ProjectData) projectData; JsonArrayBuilder teamArrayBuilder = Json.createArrayBuilder(); JsonArrayBuilder studentArrayBuilder = Json.createArrayBuilder(); ObservableList<Team> teams = projectDataManager.getTeams(); ObservableList<Student> students = projectDataManager.getStudents(); for (Team team : teams) { JsonObject teamsJson = Json.createObjectBuilder().add(JSON_NAME, team.getName()) .add(JSON_COLOR, team.getColor()).add(JSON_TEXTCOLOR, team.getTextColor()) .add(JSON_LINK, team.getLink()).build(); teamArrayBuilder.add(teamsJson); } JsonArray teamArray = teamArrayBuilder.build(); for (Student student : students) { JsonObject studentsJson = Json.createObjectBuilder().add(JSON_FIRSTNAME, student.getFirstName()) .add(JSON_LASTNAME, student.getLastName()).add(JSON_TEAM, student.getTeam()) .add(JSON_ROLE, student.getRole()).build(); studentArrayBuilder.add(studentsJson); } JsonArray studentArray = studentArrayBuilder.build(); CourseData courseDataManager = (CourseData) courseData; JsonArrayBuilder courseArrayBuilder = Json.createArrayBuilder(); ObservableList<CourseTemplate> templates = courseDataManager.getTemplates(); for (CourseTemplate template : templates) { JsonObject courseJson = Json.createObjectBuilder().add(JSON_USE, template.isUse().getValue()) .add(JSON_NAVBAR, template.getNavbarTitle()).add(JSON_FILENAME, template.getFileName()) .add(JSON_SCRIPT, template.getScript()).build(); courseArrayBuilder.add(courseJson); } JsonArray courseArray = courseArrayBuilder.build(); JsonObject courseJson = Json.createObjectBuilder().add(JSON_SUBJECT, courseDataManager.getSubject()) .add(JSON_NUMBER, courseDataManager.getNumber()).add(JSON_SEMESTER, courseDataManager.getSemester()) .add(JSON_YEAR, courseDataManager.getYear()).add(JSON_TITLE, courseDataManager.getTitle()) .add(JSON_INSTRUCTORNAME, courseDataManager.getInsName()) .add(JSON_INSTRUCTORHOME, courseDataManager.getInsHome()) .add(JSON_BANNER, courseDataManager.getBannerLink()) .add(JSON_LEFTFOOTER, courseDataManager.getLeftFooterLink()) .add(JSON_RIGHTFOOTER, courseDataManager.getRightFooterLink()) .add(JSON_EXPORTDIR, courseDataManager.getExportDir()) .add(JSON_TEMPLATEDIR, courseDataManager.getTemplateDir()) .add(JSON_STYLESHEET, courseDataManager.getStyleSheet()).build(); // THEN PUT IT ALL RECITATIONS IN A JsonObject JsonObject dataManagerJSO = Json.createObjectBuilder().add(JSON_COURSE, courseJson) .add(JSON_COURSETEMPLATE, courseArray).add(JSON_START_HOUR, "" + dataManager.getStartHour()) .add(JSON_END_HOUR, "" + dataManager.getEndHour()).add(JSON_UNDERGRAD_TAS, undergradTAsArray) .add(JSON_OFFICE_HOURS, timeSlotsArray).add(JSON_RECITATION, recitaitonArray) .add(JSON_STARTDAY, courseDataManager.getStartDay()) .add(JSON_STARTMONTH, courseDataManager.getStartMonth()) .add(JSON_STARTYEAR, courseDataManager.getStartYear()) .add(JSON_ENDDAY, courseDataManager.getEndDay()).add(JSON_ENDMONTH, courseDataManager.getEndMonth()) .add(JSON_ENDYEAR, courseDataManager.getEndYear()).add(JSON_SCHEDULEITEM, scheduleArray) .add(JSON_TEAMS, teamArray).add(JSON_STUDENTS, studentArray).build(); // AND NOW OUTPUT IT TO A JSON FILE WITH PRETTY PRINTING Map<String, Object> properties = new HashMap<>(1); properties.put(JsonGenerator.PRETTY_PRINTING, true); JsonWriterFactory writerFactory = Json.createWriterFactory(properties); StringWriter sw = new StringWriter(); JsonWriter jsonWriter = writerFactory.createWriter(sw); jsonWriter.writeObject(dataManagerJSO); jsonWriter.close(); // INIT THE WRITER OutputStream os = new FileOutputStream(filePath); JsonWriter jsonFileWriter = Json.createWriter(os); jsonFileWriter.writeObject(dataManagerJSO); String prettyPrinted = sw.toString(); PrintWriter pw = new PrintWriter(filePath); pw.write(prettyPrinted); pw.close(); }
From source file:org.hyperledger.fabric_ca.sdk.HFCAClient.java
String toJson(JsonObject toJsonFunc) { StringWriter stringWriter = new StringWriter(); JsonWriter jsonWriter = Json.createWriter(new PrintWriter(stringWriter)); jsonWriter.writeObject(toJsonFunc);// ww w .ja v a 2 s . c om jsonWriter.close(); return stringWriter.toString(); }
From source file:org.hyperledger.fabric_ca.sdk.HFCAClient.java
/** * Generate certificate revocation list. * * @param registrar admin user configured in CA-server * @param revokedBefore Restrict certificates returned to revoked before this date if not null. * @param revokedAfter Restrict certificates returned to revoked after this date if not null. * @param expireBefore Restrict certificates returned to expired before this date if not null. * @param expireAfter Restrict certificates returned to expired after this date if not null. * @throws InvalidArgumentException/* ww w . j av a 2s . c om*/ */ public String generateCRL(User registrar, Date revokedBefore, Date revokedAfter, Date expireBefore, Date expireAfter) throws InvalidArgumentException, GenerateCRLException { if (cryptoSuite == null) { throw new InvalidArgumentException("Crypto primitives not set."); } if (registrar == null) { throw new InvalidArgumentException("registrar is not set"); } try { setUpSSL(); //--------------------------------------- JsonObjectBuilder factory = Json.createObjectBuilder(); if (revokedBefore != null) { factory.add("revokedBefore", Util.dateToString(revokedBefore)); } if (revokedAfter != null) { factory.add("revokedAfter", Util.dateToString(revokedAfter)); } if (expireBefore != null) { factory.add("expireBefore", Util.dateToString(expireBefore)); } if (expireAfter != null) { factory.add("expireAfter", Util.dateToString(expireAfter)); } if (caName != null) { factory.add(HFCAClient.FABRIC_CA_REQPROP, caName); } JsonObject jsonObject = factory.build(); StringWriter stringWriter = new StringWriter(); JsonWriter jsonWriter = Json.createWriter(new PrintWriter(stringWriter)); jsonWriter.writeObject(jsonObject); jsonWriter.close(); String body = stringWriter.toString(); //--------------------------------------- // send revoke request JsonObject ret = httpPost(url + HFCA_GENCRL, body, registrar); return ret.getString("CRL"); } catch (Exception e) { logger.error(e.getMessage(), e); throw new GenerateCRLException(e.getMessage(), e); } }
From source file:org.jboss.set.aphrodite.stream.services.json.JsonStreamService.java
@Override public void serializeStreams(URL url, OutputStream out) throws NotFoundException { final Collection<Stream> streams = this.urlToParsedStreams.get(url); if (streams == null) { throw new NotFoundException("No matching set of streams for '" + url + "'"); }//from w ww . ja v a2 s .c o m JsonObject jsonObject = StreamsJsonParser.encode(streams); // JsonWriter jsonWriter = Json.createWriter(out); // jsonWriter.writeObject(jsonObject); // jsonWriter.close(); Map<String, Boolean> config = buildConfig(); JsonWriterFactory writerFactory = Json.createWriterFactory(config); JsonWriter jsonWriter = writerFactory.createWriter(out); jsonWriter.write(jsonObject); jsonWriter.close(); }