Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions contributions/javaGsonObjectsJava10/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.class
.gradle/
.settings/
.classpath
.project
bin/
build/
libs/
outputs/
128 changes: 128 additions & 0 deletions contributions/javaGsonObjectsJava10/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
This is an implementation of the 101companies System.

== Headline ==
Working on [[Language:JSON|]] with Google [[Technology:Gson|Gson API]], mapping Json to a direct object representation

== Characteristics ==
This contribution modernizes the [[Contribution:javaGsonObjects]] (which is a simple company structure, displaying the serialization process using [[Language:JSON]] for storing the company structure and Gson API for parsing, unparsing and manipulating it the JSON.), by utilizing new features, introduced up to Java10, such as [[Concept:Lambda abstraction]] and [[Concept:Streaming]].

== Illustration ==
To Parse a JSON File you can either use "parseFromFile(String path)" or "parseFromFile(File file)"
The former is only a wrapper method for the latter, so we'll be taking a look at the "parseFromFile(File file)" method.
<syntaxhighlight lang="java">
/**
* Method to parse a JSON file to a Company
*
* @param file Path for JSON file
* @return a Company representing the JSON file
* @throws IOException
* @throws FileNotFoundException
*/
public static Company parseFromFile(File file) throws FileNotFoundException, IOException {
Gson gson = new Gson();
try (JsonReader read = new JsonReader(new FileReader(file))) {
return gson.fromJson(read, Company.class);
}
}
</syntaxhighlight>
Given a JSON file, the method will create a Gson object and attempt to read the file using a JsonReader.
Should the file not be found, the method will throw a FileNotFoundException.
Were there problems reading the File, then an IOException will be thrown.
The method will proceed to the last instuction if the file was read without any problems.
<code>gson.fromJson(read, Company.class);</code>
Here, the "fromJson" method of the gson object, is parsing the read JSON data into an object of type Company.
Alternatively, you can call the "parse(String jsonString)" method directly, if you have already read the JSON file into a string.
<syntaxhighlight lang="java">
/**
* Method to parse a JSON String to a Company
*
* @param jsonString String containing JSON
* @return a Company representing the JSON from the jsonString
*/
public static Company parse(String jsonString) {
Gson gson = new Gson();
return gson.fromJson(jsonString, Company.class);
}
</syntaxhighlight>

The Unparsing methods are very similar to the parsing ones.
Like in the Parsing class, we defined a wrapper function for the parameter path.
The "unparseToFile(Company company, File file)" method looks as follows:
<syntaxhighlight lang="java">
/**
* Method to parse a Company to a JSON File
*
* @param company Company to be parsed to a JSON File
* @param file Destination File for the JSON File
* @throws IOException
*/
public static void unparseToFile(Company company, File file) throws IOException {
GsonBuilder builder = new GsonBuilder();
builder.setPrettyPrinting();
Gson gson = builder.create();
try (FileWriter writer = new FileWriter(file)) {
gson.toJson(company, writer);
}
}
</syntaxhighlight>
Let us take a closer look into it's inner workings.
The line...
<code> GsonBuilder builder = new GsonBuilder();</code>
...leaves us with a GsonBuilder object, which will serve the purpose of creating the gson object we'll be using to create and write the JSON file with.
We then proceed in creating a FileWriter object via a try-with-resources statement.
<syntaxhighlight lang="java">
try (FileWriter writer = new FileWriter(file)) {
gson.toJson(c, writer);
}
</syntaxhighlight>
The FileWriter is then used by the gson object to write the JSON representation of the company into the file given by the user.

The string representation of a company object in the JSON format can be obtained directly, without being written to a file, by the "unparse(Company company)" method.
<syntaxhighlight lang="java">
/**
* Method to parse a Company to a JSON String
*
* @param company Company to be parsed to a JSON File
* @return a String representing the JSON File of the company
*/
public static String unparse(Company company) {
GsonBuilder builder = new GsonBuilder();
builder.setPrettyPrinting();
Gson gson = builder.create();
return gson.toJson(company);
}
</syntaxhighlight>

== Relationships ==
[[Contribution:javaGsonObjects] is the base for this contribution.
[[Contribution:javaGson]] is using the same Api and Features with a different JSON mapping.

== Architecture ==
The contribution follows a standardized structure:
* inputs contains input files for tests
* src/main/java contains the following packages:
** org.softlang.company.model for implementations of [[Feature:Hierarchical company]].
** org.softlang.company.features for implementations of [[Namespace:Feature]].
* src/test/java contains the following packages:
** org.softlang.company.tests for [[Technology:JUnit]] test cases for [[Namespace:Feature]]s.

== Usage ==
This contribution uses [[Technology:Gradle]] for building. [[Technology:Eclipse]] is supported.
See: https://github.com/101companies/101simplejava/blob/master/README.md

== Metadata ==
* [[developedBy::Contributor:mpaul138]]
* [[implements::Feature:Hierarchical company]]
* [[implements::Feature:Unparsing]]
* [[implements::Feature:Parsing]]
* [[implements::Feature:Total]]
* [[implements::Feature:Cut]]
* [[implements::Feature:Mapping]]
* [[implements::Feature:Serialization]]
* [[memberOf::Theme:Java mapping]]
* [[uses::Language:JSON]]
* [[uses::Language:Plain Text]]
* [[uses::Language:Java]]
* [[uses::Technology:Gson]]
* [[uses::Technology:JUnit]]
* [[uses::Technology:Gradle]]
26 changes: 26 additions & 0 deletions contributions/javaGsonObjectsJava10/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apply plugin: 'java'
apply plugin: 'eclipse'

group = 'org.softlang.company'
sourceCompatibility = JavaVersion.VERSION_1_10
targetCompatibility = JavaVersion.VERSION_1_10
version = '1.0.0'

// Implementation specifics
// This implementation uses JUnit for testing
dependencies {
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.3.1'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.3.1'
}
// Additional cleanup for outputs
clean {
dependsOn cleanEclipse
doFirst {
delete 'outputs'
}
}
// JUnit 5 support
test {
useJUnitPlatform()
}
41 changes: 41 additions & 0 deletions contributions/javaGsonObjectsJava10/inputs/sampleCompany.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "Acme Corporation",
"departments": [
{
"name": "Research",
"manager": {
"name": "Fred",
"salary": 88888.0
}
},
{
"name": "Development",
"manager": {
"name": "Marie",
"salary": 77777.0
},
"departments": [
{
"name": "Dev1",
"manager": {
"name": "Bob",
"salary": 77776.0
}
},
{
"name": "Dev2",
"manager": {
"name": "Alice",
"salary": 77775.0
},
"employees": [
{
"name": "Ralf",
"salary": 4711.0
}
]
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.softlang.company.features;

import java.util.Optional;

import org.softlang.company.model.Company;
import org.softlang.company.model.Department;
import org.softlang.company.model.Employee;

public class Cut {

/**
* Method to cut salaries in half
*
* @param company Company to cut salaries from
*/
public static void cut(Company company) {
Optional.ofNullable(company).map(Company::getDepartments)
.ifPresent(departments -> departments.forEach(Cut::cut));
}

/**
* Helper method for cut
*
* @param department Department to cut salaries from
*/
private static void cut(Department department) {
Optional.ofNullable(department.getManager()).ifPresent(Cut::cut);

Optional.ofNullable(department.getEmployees()).ifPresent(employees -> employees.forEach(Cut::cut));

Optional.ofNullable(department.getDepartments()).ifPresent(departments -> departments.forEach(Cut::cut));
}

/**
* Helper method for cut
*
* @param employee Employee to cut salary from
*/
private static void cut(Employee employee) {
employee.setSalary(employee.getSalary() / 2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.softlang.company.features;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.softlang.company.model.Company;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;

public class Parsing {

/**
* Method to parse a JSON file to a Company
*
* @param path Path to the JSON file
* @return a Company representing the JSON file
* @throws IOException
* @throws FileNotFoundException
*/
public static Company parseFromFile(String path) throws FileNotFoundException, IOException {
return parseFromFile(new File(path));
}

/**
* Method to parse a JSON file to a Company
*
* @param file Path for JSON file
* @return a Company representing the JSON file
* @throws IOException
* @throws FileNotFoundException
*/
public static Company parseFromFile(File file) throws FileNotFoundException, IOException {
Gson gson = new Gson();
try (JsonReader read = new JsonReader(new FileReader(file))) {
return gson.fromJson(read, Company.class);
}
}

/**
* Method to parse a JSON String to a Company
*
* @param jsonString String containing JSON
* @return a Company representing the JSON from the jsonString
*/
public static Company parse(String jsonString) {
Gson gson = new Gson();
return gson.fromJson(jsonString, Company.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.softlang.company.features;

import java.util.Optional;

import org.softlang.company.model.Company;
import org.softlang.company.model.Department;
import org.softlang.company.model.Employee;

public class Total {

/**
* Method to get the total of all salaries
*
* @param company Company to compute total for
* @return total of Company company
*/
public static double total(Company company) {
return Optional.ofNullable(company).map(Company::getDepartments)
.map(departments -> departments.stream().mapToDouble(Total::total).sum()).orElse(0.0);
}

/**
* Helper Method to get the total of all salaries in a Department
*
* @param department Department to compute the total for
* @return total of all salaries in Department department
*/
private static double total(Department d) {
double sum = Optional.ofNullable(d.getManager()).map(Employee::getSalary).orElse(0.0);

sum += Optional.ofNullable(d.getEmployees())
.map(employees -> employees.stream().mapToDouble(Employee::getSalary).sum()).orElse(0.0);

return sum + Optional.ofNullable(d.getDepartments())
.map(departments -> departments.stream().mapToDouble(Total::total).sum()).orElse(0.0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.softlang.company.features;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.softlang.company.model.Company;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class Unparsing {

/**
* Method to parse a Company to a JSON File
*
* @param company Company to be parsed to a JSON File
* @param path Destination Path for the JSON File
* @throws IOException
*/
public static void unparseToFile(Company comapny, String path) throws IOException {
unparseToFile(comapny, new File(path));
}

/**
* Method to parse a Company to a JSON File
*
* @param company Company to be parsed to a JSON File
* @param file Destination File for the JSON File
* @throws IOException
*/
public static void unparseToFile(Company company, File file) throws IOException {
GsonBuilder builder = new GsonBuilder();
builder.setPrettyPrinting();
Gson gson = builder.create();
try (FileWriter writer = new FileWriter(file)) {
gson.toJson(company, writer);
}
}

/**
* Method to parse a Company to a JSON String
*
* @param company Company to be parsed to a JSON File
* @return a String representing the JSON File of the company
*/
public static String unparse(Company company) {
GsonBuilder builder = new GsonBuilder();
builder.setPrettyPrinting();
Gson gson = builder.create();
return gson.toJson(company);
}

}
Loading