Example usage for javax.json JsonWriter writeObject

List of usage examples for javax.json JsonWriter writeObject

Introduction

In this page you can find the example usage for javax.json JsonWriter writeObject.

Prototype

void writeObject(JsonObject object);

Source Link

Document

Writes the specified JSON JsonObject object to the output source.

Usage

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;//from   w w  w .java 2 s .  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);
    jsonWriter.close();/*www  .  j  a v  a  2  s  .c  o  m*/
    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//from  w  ww. ja v  a2s  . com
 */

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);
    }
}