ConfigurationSerializable small question

Discussion in 'Plugin Development' started by Smerfa, Jun 24, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Smerfa

    if I using ConfigurationSerializable in class "MyClass"
    Then if I have that "valueOf(Map<String, Object> map)" method I can just use
    MyClass myClass = (MyClass) config.get("path.to.myclass");
    ?

    I always creating own systems for that :D

    And second small question... that will work fine? or map must be "flat"
    Code:java
    1. @Override
    2. public Map<String, Object> serialize()
    3. {
    4. TreeMap<String, Object> map = new TreeMap<>();
    5. map.put("owner", owner);
    6. map.put("name", name);
    7. map.put("num", num);
    8. map.put("type", type.name());
    9. map.put("bonus", bonus);
    10. TreeMap<String, Object> limits = new TreeMap<>(); // can I create map in map here?
    11. limits.put("superSizeUp", superSizeUp);
    12. limits.put("superSizeDown", superSizeDown);
    13. map.put("limits", limits);
    14. return limits;
    15. }
     
  2. Offline

    mythbusterma

    Yes, that is how you use the ConfigurationSerializable, and yes, the code will work, except the method should return "map" instead of "limit" (assuming your valueof() method is set up correctly).
     
    Smerfa likes this.
  3. Offline

    Smerfa

    Hah, IDK why I have "limits" here :D

    And one more question about de-serializing
    If i have primitive field like "int"
    then i should:
    this.myInt = (int) map.get("myInt");
    or
    this.myInt = (Integer) map.get("myInt");

    Or any of that will work? (IMHO any of that should work, but I don't want changing 10372 lines of code if that will throw error :D)
     
  4. Offline

    mythbusterma

    It would be stored as an Integer, because a map can't address an int, but either would work, so it's up to you (I would go with "Integer" though).
     
    Smerfa likes this.
  5. Offline

    1Rogue

    int and Integer are autoboxable, you don't need to cast between the two (though I suppose that's irrelevant because the map returns an Object).

    You should use Integer first as the result may be null, and if it is assigning the null to a primitive value will throw an NPE.
     
  6. Offline

    Smerfa

    I know that I don't must cast between int and Integer :)


    One more question...
    That will work with lists?
    Code:
    MyList:
      - owner: Notch
        name: myHome
        num: 1
        type: NORMAL
        bonus: false
        limits:
          superSizeUp: 0
          superSizeDown: 0
      - owner: _Jeb
        name: farm
        num: 1
        type: VIP
        bonus: false
        limits:
          superSizeUp: 20
          superSizeDown: 2
      - owner: Other
        name: yep
        num: 34
        type: CREATIVE
        bonus: true
        limits:
          superSizeUp: 0
          superSizeDown: 5
    To save it form List<MyClass> and load it by
    List<MyClass> mylist = (List<MyClass>) getList("MyList");
     
  7. Offline

    1Rogue


    It would need the class type in there so it knows what it's deserializing. Try saving a list first to see the format.
     
Thread Status:
Not open for further replies.

Share This Page