Skip to content

Instantly share code, notes, and snippets.

@hamnis
Last active July 31, 2016 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamnis/a03f1b766d4b7ee72528eff031f8b17f to your computer and use it in GitHub Desktop.
Save hamnis/a03f1b766d4b7ee72528eff031f8b17f to your computer and use it in GitHub Desktop.
package net.hamnaberg.json.examples;
import javaslang.collection.List;
import javaslang.control.Option;
import net.hamnaberg.json.Codecs;
import net.hamnaberg.json.DecodeResult;
import net.hamnaberg.json.Json;
import net.hamnaberg.json.JsonCodec;
import net.hamnaberg.json.io.JacksonStreamingParser;
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
Person p1 = new Person("John Doe", Option.some(LocalDate.of(1981, 1, 1)), List.empty());
System.out.println("p1 = " + p1);
Json.JValue data = new JacksonStreamingParser().parse(Main.class.getResourceAsStream("/person.json"));
System.out.println("data = " + data);
DecodeResult<Person> decodePerson = MyExtractors.person().apply(data.asJsonObjectOrEmpty());
System.out.println("decodePerson = " + decodePerson);
DecodeResult<Person> decodePerson2 = MyExtractors.person().decoder().fromJson(data);
System.out.println("decodePerson2 = " + decodePerson2);
JsonCodec<LocalDate> localDateJsonCodec = Codecs.StringCodec.tryNarrow(LocalDate::parse, LocalDate::toString);
JsonCodec<Person> personcodec = Codecs.codec3(PersonIso.INSTANCE, Codecs.StringCodec, Codecs.OptionCodec(localDateJsonCodec), Codecs.listCodec(Codecs.StringCodec)).
apply("name", "birthDate", "interests");
DecodeResult<Person> person3 = personcodec.fromJson(data);
Option<Json.JValue> data2 = personcodec.toJson(person3.unsafeGet());
System.out.println("data2 = " + data2);
/* Outputs:
p1 = Person{name='John Doe', birthDate=Some(1981-01-01), interests=List()}
data = JObject{value={name=JString{value='John Doe'}, birthDate=JString{value='1982-01-01'}, interests=JArray{value=List()}}}
decodePerson = Ok(value='Person{name='John Doe', birthDate=Some(1982-01-01), interests=List()}')
decodePerson2 = Ok(value='Person{name='John Doe', birthDate=Some(1982-01-01), interests=List()}')
data2 = Some(JObject{value={name=JString{value='John Doe'}, birthDate=JString{value='1982-01-01'}, interests=JArray{value=List()}}})
*/
}
}
package net.hamnaberg.json.examples;
import net.hamnaberg.json.Codecs;
import net.hamnaberg.json.extract.Extractor;
import net.hamnaberg.json.extract.Extractors;
import java.time.LocalDate;
import static net.hamnaberg.json.extract.TypedField.*;
public class MyExtractors {
public static Extractor<Person> person() {
return Extractors.extract3(
TString("name"),
TOptional("birthDate", Codecs.StringCodec.map(LocalDate::parse)),
TJArray("interests").mapToList(j -> j.asString().getOrElse("")),
Person::new
);
}
}
package net.hamnaberg.json.examples;
import javaslang.collection.List;
import javaslang.control.Option;
import java.time.LocalDate;
public class Person {
public final String name;
public final Option<LocalDate> birthDate;
public final List<String> interests;
public Person(String name, Option<LocalDate> birthDate, List<String> interests) {
this.birthDate = birthDate;
this.name = name;
this.interests = interests;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", birthDate=" + birthDate +
", interests=" + interests +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
if (!name.equals(person.name)) return false;
if (!birthDate.equals(person.birthDate)) return false;
return interests.equals(person.interests);
}
@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + birthDate.hashCode();
result = 31 * result + interests.hashCode();
return result;
}
}
package net.hamnaberg.json.examples;
import javaslang.Tuple3;
import javaslang.collection.List;
import javaslang.control.Option;
import net.hamnaberg.json.Iso;
import java.time.LocalDate;
public enum PersonIso implements Iso<Person, Tuple3<String, Option<LocalDate>, List<String>>> {
INSTANCE;
@Override
public Tuple3<String, Option<LocalDate>, List<String>> get(Person person) {
return new Tuple3<>(person.name, person.birthDate, person.interests);
}
@Override
public Person reverseGet(Tuple3<String, Option<LocalDate>, List<String>> t) {
return t.transform(Person::new);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment