ralfstx / minimal-json

A fast and small JSON parser and writer for Java
MIT License
732 stars 186 forks source link

Error in updating json file #119

Closed Lord-Toffa closed 4 years ago

Lord-Toffa commented 4 years ago

Hello,

first of all, thank you! You made a fantastic work with this tool. That's my first isue message in github. If it's incomprensible, pleas, tell it to me, I'll try to be more precise.

I am programming a small supermarket inteface for an Univrsity Project.

I have the follow issue when I try to change a value in my users.json file.

FROM THIS: { "users": [ {

        "email": "a",
        "password": "a",
        "name":"User",
        "familyname":"Debugging",
        "address":"Via bestemmie, 666",
        "city":"Quel Paese",
        "CAP":"66420",
        "mobilenumber": "4136181111",
        "fidelitycard":"null",
        "userid":0
    }
]

}

TO THIS: [ { "email": "a", "password": "a", "name": "User", "familyname": "Debugging", "address": "Via bestemmie, 666", "city": "Quel Paese", "CAP": "66420", "mobilenumber": "4136181111", "fidelitycard": "", "userid": 0 }, ]

We decide to re-write the inteire file insted of using a get/set function. I'll post the code of the class which modify the json file:

public static void saveUser() { JsonArray users = new JsonArray();

    for (User u : Globals.users) {

        JsonObject jsonUser = new JsonObject();

        jsonUser.add("email", u.getEmail());

        jsonUser.add("password", u.getPassword());

        jsonUser.add("name", u.getAnagrafica().getName());

        jsonUser.add("familyname", u.getAnagrafica().getFamilyName());

        jsonUser.add("address", u.getAnagrafica().getAddress());

        jsonUser.add("city", u.getAnagrafica().getCity());

        jsonUser.add("CAP", u.getAnagrafica().getCAP());

        jsonUser.add("mobilenumber", u.getAnagrafica().getMobileNumber());

        jsonUser.add("fidelitycard", "");

        jsonUser.add("userid", u.getUserID());

        users.add(jsonUser);

        System.out.println(jsonUser);
    }

    System.out.println("\n[?] " + users);

    try (Writer writer = new FileWriter("./data/users.json")) {
        String json = users.toString(WriterConfig.PRETTY_PRINT);
        users.writeTo(writer, WriterConfig.PRETTY_PRINT);
    } catch (IOException e) {
        System.out.println("[x] Errore scrittura Json User!!!");
    }

}

Essentially my problem is:

I'll miss the:

{ "users": [ {

at the beginning of my file... Is possible to resolve that? have i made an error while writing my code?

ralfstx commented 4 years ago

@Lord-Toffa You should file GitHub issues when you're convinced that there's something wrong in minimal-json, together with detailed instructions to reproduce the error, or to request new features. This rather seems like a question for Stack overflow.