Commits

Hector Miuler Malpica Gallegos  committed 4c79d3c

pasando las clases a java

  • Participants
  • Parent commits 0363e25

Comments (0)

Files changed (6)

File src/main/scala/JacksonMain.scala

-
-
-
-import com.fasterxml.jackson.databind.ObjectMapper
-import com.fasterxml.jackson.module.scala.DefaultScalaModule
-
-
-object JacksonMain extends App {
-
-  val mapper = new ObjectMapper
-  mapper.registerModule(DefaultScalaModule)
-
-
-  println("==============================")
-  pruebaMapperMap
-  println("==============================")
-  pruebaMapperClass
-  println("==============================")
-
-
-  def pruebaMapperMap {
-
-    val map = Map("uno" -> 1, "dos" -> BigDecimal("5.102"), "tres" -> "tres")
-
-    val out = mapper.writeValueAsString(map)
-
-    println(map("dos").getClass.getSimpleName)
-    println(out)
-
-    val obj = mapper.readValue(out, classOf[Map[String, Any]])
-    println(obj)
-    println(obj("dos").getClass.getSimpleName)
-  }
-
-  def pruebaMapperClass {
-
-    val person = Person("Miuler", 33)
-
-    val out = mapper.writeValueAsString(person)
-
-    println(out)
-  }
-
-}
-
-case class Person(name:String, age:Int) extends Serializable

File src/main/scala/miuler/demo/jackson/JacksonMain.scala

+package miuler.demo.jackson
+
+
+import com.fasterxml.jackson.databind.{JsonDeserializer, ObjectMapper}
+import com.fasterxml.jackson.module.scala.DefaultScalaModule
+import miuler.demo.jackson.domain.{Boss, Person, Employee}
+
+
+object JacksonMain extends App {
+
+  val mapper = new ObjectMapper
+  mapper.registerModule(DefaultScalaModule)
+  mapper.enableDefaultTyping()
+
+
+  println("==============================")
+//  pruebaMapperMap
+  println("==============================")
+  pruebaMapperClass
+  println("==============================")
+
+
+  def pruebaMapperMap {
+
+    val map = Map("uno" -> 1, "dos" -> BigDecimal("5.102"), "tres" -> "tres")
+
+    val out = mapper.writeValueAsString(map)
+
+    println(map("dos").getClass.getSimpleName)
+    println(out)
+
+    val obj = mapper.readValue(out, classOf[Map[String, Any]])
+    println(obj)
+    println(obj("dos").getClass.getSimpleName)
+  }
+
+  def pruebaMapperClass {
+
+    import miuler.demo._
+
+    val person = new Employee("Miuler", 33, "AEP")
+    val out1 = mapper.writeValueAsString(person)
+    println(out1)
+
+    val out2 = mapper.readValue(out1, classOf[Person])
+    println(out2)
+  }
+
+  def testHerencia {
+
+    import miuler.demo._
+
+    val prueba:Person = new Boss("Miuler", 34, "AEP")
+    println(prueba)
+    prueba.isInstanceOf[Boss]
+    val boss = prueba.asInstanceOf[Boss]
+    boss.getOrganization
+
+    prueba match {
+      case boss:Boss => println(boss.getOrganization)
+    }
+  }
+
+}
+
+

File src/main/scala/miuler/demo/jackson/JacksonMainJ.java

+package miuler.demo.jackson;
+
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.module.scala.DefaultScalaModule;
+import miuler.demo.jackson.domain.Employee;
+import miuler.demo.jackson.domain.Person;
+
+import java.io.IOException;
+
+/**
+ * @author: miuler
+ * <p/>
+ * date: 15/11/13 time: 11:53
+ */
+public class JacksonMainJ {
+
+    public static void main(String [] args) throws IOException {
+        ObjectMapper mapper = new ObjectMapper();
+//        mapper.enableDefaultTyping();
+
+        System.out.println("Hola");
+
+        Employee employee = new Employee();
+        employee.setName("Miuler");
+        employee.setAge(33);
+        employee.setOrganization("AEP");
+
+        String out1 = mapper.writeValueAsString(employee);
+        System.out.println(out1);
+
+        Person out2 = mapper.readValue(out1, Person.class);
+        System.out.println(out2);
+    }
+}

File src/main/scala/miuler/demo/jackson/domain/Boss.java

+package miuler.demo.jackson.domain;
+
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+
+/**
+ * @author: miuler
+ * <p/>
+ * date: 15/11/13 time: 11:40
+ */
+@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property = "@class")
+public class Boss extends Person {
+
+    String organization;
+
+    public Boss() {
+
+    }
+
+    public Boss(String name, Integer age, String organization) {
+        super(name, age);
+        this.organization = organization;
+    }
+
+    public String getOrganization() {
+        return organization;
+    }
+
+    public void setOrganization(String organization) {
+        this.organization = organization;
+    }
+}

File src/main/scala/miuler/demo/jackson/domain/Employee.java

+package miuler.demo.jackson.domain;
+
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+
+/**
+ * @author: miuler
+ * <p/>
+ * date: 15/11/13 time: 11:40
+ */
+@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property = "@class")
+public class Employee extends Person {
+    String organization;
+
+    public Employee() {
+
+    }
+
+    public Employee(String name, Integer age, String organization) {
+        super(name, age);
+        this.organization = organization;
+    }
+
+    public String getOrganization() {
+        return organization;
+    }
+
+    public void setOrganization(String organization) {
+        this.organization = organization;
+    }
+}

File src/main/scala/miuler/demo/jackson/domain/Person.java

+package miuler.demo.jackson.domain;
+
+import com.fasterxml.jackson.annotation.JsonSubTypes;
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+
+/**
+ * @author: miuler
+ * <p/>
+ * date: 15/11/13 time: 11:38
+ */
+@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property = "@class")
+public class Person {
+
+    String name;
+    Integer age;
+
+    public Person() {
+    }
+
+    public Person(String name, Integer age) {
+        this.name = name;
+        this.age = age;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Integer getAge() {
+        return age;
+    }
+
+    public void setAge(Integer age) {
+        this.age = age;
+    }
+}