Re: [jsonbeans] Trubble running the tutorial

127 views
Skip to first unread message

Nate

unread,
Dec 17, 2012, 8:23:39 PM12/17/12
to jsonbea...@googlegroups.com
Hi, sorry about that. The download was a little but out of date from the source in SVN. I've made a new release, please try v0.4 and it will work. See the example below.
https://code.google.com/p/jsonbeans/downloads/list

-Nate


import java.util.ArrayList;

public class Snippet {
    static public class Person {
        private String name;
        private int age;
        private ArrayList numbers;

        public String getName () {
            return name;
        }

        public void setName (String name) {
            this.name = name;
        }

        public int getAge () {
            return age;
        }

        public void setAge (int age) {
            this.age = age;
        }

        public ArrayList getNumbers () {
            return numbers;
        }

        public void setNumbers (ArrayList numbers) {
            this.numbers = numbers;
        }

    }

    static public class PhoneNumber {
        private String name;
        private String number;

        public PhoneNumber () {
        }

        public PhoneNumber (String name, String number) {
            this.name = name;
            this.number = number;
        }

        public String getName () {
            return name;
        }

        public void setName (String name) {
            this.name = name;
        }

        public String getNumber () {
            return number;
        }

        public void setNumber (String number) {
            this.number = number;
        }

    }

    public static void main (String[] args) throws Exception {
        Person person = new Person();
        person.setName("Nate");
        person.setAge(31);
        ArrayList numbers = new ArrayList();
        numbers.add(new PhoneNumber("Home", "206-555-1234"));
        numbers.add(new PhoneNumber("Work", "425-555-4321"));
        person.setNumbers(numbers);

        Json json = new Json();
        System.out.println(json.prettyPrint(person));
    }
}





On Sun, Dec 16, 2012 at 9:15 AM, Johan Öhman <johan....@gmail.com> wrote:
I'm trying to get the tutorial to run, but I get exception. Anyone know what I have missed?

Exception in thread "main" com.esotericsoftware.jsonbeans.JsonException: Serialization trace:
private java.util.ArrayList vision.gravity.universe.track.treesystem.Person.numbers (vision.gravity.universe.track.treesystem.Person)
at com.esotericsoftware.jsonbeans.Json.writeFields(Json.java:234)
at com.esotericsoftware.jsonbeans.Json.writeValue(Json.java:438)
at com.esotericsoftware.jsonbeans.Json.toJson(Json.java:200)
at com.esotericsoftware.jsonbeans.Json.toJson(Json.java:158)
at com.esotericsoftware.jsonbeans.Json.toJson(Json.java:149)
at com.esotericsoftware.jsonbeans.Json.prettyPrint(Json.java:825)
at com.esotericsoftware.jsonbeans.Json.prettyPrint(Json.java:817)
at vision.gravity.universe.track.treesystem.Example.main(Example.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.IllegalStateException: Name must be set.
at com.esotericsoftware.jsonbeans.JsonWriter.object(JsonWriter.java:60)
at com.esotericsoftware.jsonbeans.Json.writeObjectStart(Json.java:473)
at com.esotericsoftware.jsonbeans.Json.writeValue(Json.java:437)
at com.esotericsoftware.jsonbeans.Json.writeValue(Json.java:383)
at com.esotericsoftware.jsonbeans.Json.writeFields(Json.java:227)
... 12 more

Code I used:
  Person person = new Person();
    person.setName("Nate");
    person.setAge(31);
    ArrayList numbers = new ArrayList();
    numbers.add(new PhoneNumber("Home", "206-555-1234"));
    numbers.add(new PhoneNumber("Work", "425-555-4321"));
    person.setNumbers(numbers);

    Json json = new Json();
    System.out.println(json.prettyPrint(person));

I'm using the jasonbeans-0.3.

/Johan


--
You received this message because you are subscribed to the "jsonbeans-users" group:
http://groups.google.com/group/jsonbeans-users

Johan Öhman

unread,
Dec 19, 2012, 12:36:38 PM12/19/12
to jsonbea...@googlegroups.com
Perfect, it works. Thanks!

I'm trying to get rid of Jackson and using jsonbeans instead for our level data and editor. 

/Johan

Nate

unread,
Dec 19, 2012, 6:08:04 PM12/19/12
to jsonbea...@googlegroups.com
Nice. :) Hopefully you find JsonBeans easy to use. You are making a game? Have you seen libgdx?
http://libgdx.badlogicgames.com/
I originally developed JsonBeans there, so it is built-in. I only ported it to a separate project for use outside of libgdx.

-Nate


Reply all
Reply to author
Forward
0 new messages