Thread: TXT to JSON converter

Results 1 to 4 of 4
  1. #1 TXT to JSON converter 
    Banned

    Join Date
    Oct 2012
    Posts
    4,716
    Thanks given
    1,682
    Thanks received
    1,105
    Rep Power
    0
    Hi there,

    How much would i be looking at?

    [Only registered and activated users can see links. ]

    Kind regards,

    Patrick
     

  2. #2  
    zen2
    Corey's Avatar
    Join Date
    Feb 2012
    Age
    23
    Posts
    1,388
    Thanks given
    1,090
    Thanks received
    1,450
    Discord
    View profile
    Rep Power
    5000
    Look at some GSON tutorials. You're a smart guy, it's pretty easy.
    [Only registered and activated users can see links. ]
     

  3. #3  
    Wut can u say when theres nothin to tell

    Tyrant's Avatar
    Join Date
    Jul 2013
    Age
    21
    Posts
    1,535
    Thanks given
    668
    Thanks received
    402
    Rep Power
    971
    Assuming your .txt is some definitions, as long as the object that the definitions data are loaded into is Serializable (usually is), you can simply use Gson utilities to perform this easily.
    For example, if this is how your definition object looks like:

    Code:
    class ItemDefinition {
    final String name;
    final int id;
    ...
    
    //construction isn't necessary, since it deserialize by the fields (thus transient will not be supported, unless indicated otherwise)
    ItemDefinition(String name, int id) {
    this.name = name;
    this.id = id;
    }
    
    }
    Say this is your current definitions, this is the methods you'll need to workout with:

    serialize
    Code:
           /*private static final*/Path PATH = Paths.get("data", "def", "items");
            File fileToSave = new File(PATH.normalize().toFile().getAbsoluteFile() + File.separator + fileName + ".json");
            if (!fileToSave.exists()) {
                try {
                    if (!fileToSave.createNewFile()) {
                        throw new IllegalStateException("Couldn't create file for item" + fileToSave.getName());
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try (FileWriter fw = new FileWriter(fileToSave)) {
                fw.write(GSON.toJson(STORE_HERE_WHATEVER_OBJECT)); //i.e ItemDefinitions.get(id)
                System.out.println("written to file " + mobDrop);
            } catch (Exception e) {
                e.printStackTrace();
            }

    deserialize
    Code:
    
           /*private static final*/Path PATH = Paths.get("data", "def", "items");
            File[] files = PATH.toFile().listFiles();
            if (files != null && files.length > 0) {
                for (File f : files) {
                    if (f == null || f.isDirectory()) continue;
                    try (FileReader reader = new FileReader(f)) {
                        ItemDefinition item= GSON.fromJson(reader, new TypeToken<ItemDefinition >() {
                        }.getType());
                        //item is deserialized (loaded),  you can now use it 
                        ItemDefinitions.put( item);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                throw new IllegalStateException("No Items loaded.");
            }

    If you'd like further and more proper explaination I'l'l gladly assist if you'd pm me your discord (or quote it)



    [Only registered and activated users can see links. ]


    [Only registered and activated users can see links. ]
     

  4. #4  
    Banned

    Join Date
    Oct 2012
    Posts
    4,716
    Thanks given
    1,682
    Thanks received
    1,105
    Rep Power
    0
    Quote Originally Posted by Tyrant View Post
    Assuming your .txt is some definitions, as long as the object that the definitions data are loaded into is Serializable (usually is), you can simply use Gson utilities to perform this easily.
    For example, if this is how your definition object looks like:

    Code:
    class ItemDefinition {
    final String name;
    final int id;
    ...
    
    //construction isn't necessary, since it deserialize by the fields (thus transient will not be supported, unless indicated otherwise)
    ItemDefinition(String name, int id) {
    this.name = name;
    this.id = id;
    }
    
    }
    Say this is your current definitions, this is the methods you'll need to workout with:

    serialize
    Code:
           /*private static final*/Path PATH = Paths.get("data", "def", "items");
            File fileToSave = new File(PATH.normalize().toFile().getAbsoluteFile() + File.separator + fileName + ".json");
            if (!fileToSave.exists()) {
                try {
                    if (!fileToSave.createNewFile()) {
                        throw new IllegalStateException("Couldn't create file for item" + fileToSave.getName());
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try (FileWriter fw = new FileWriter(fileToSave)) {
                fw.write(GSON.toJson(STORE_HERE_WHATEVER_OBJECT)); //i.e ItemDefinitions.get(id)
                System.out.println("written to file " + mobDrop);
            } catch (Exception e) {
                e.printStackTrace();
            }

    deserialize
    Code:
    
           /*private static final*/Path PATH = Paths.get("data", "def", "items");
            File[] files = PATH.toFile().listFiles();
            if (files != null && files.length > 0) {
                for (File f : files) {
                    if (f == null || f.isDirectory()) continue;
                    try (FileReader reader = new FileReader(f)) {
                        ItemDefinition item= GSON.fromJson(reader, new TypeToken<ItemDefinition >() {
                        }.getType());
                        //item is deserialized (loaded),  you can now use it 
                        ItemDefinitions.put( item);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                throw new IllegalStateException("No Items loaded.");
            }

    If you'd like further and more proper explaination I'l'l gladly assist if you'd pm me your discord (or quote it)
    Its a player save, and with this i'll probably give it a try thanks guys.

    Quote Originally Posted by Tesla View Post
    Look at some GSON tutorials. You're a smart guy, it's pretty easy.
    Quote Originally Posted by Tyrant View Post
    Assuming your .txt is some definitions, as long as the object that the definitions data are loaded into is Serializable (usually is), you can simply use Gson utilities to perform this easily.
    For example, if this is how your definition object looks like:

    Code:
    class ItemDefinition {
    final String name;
    final int id;
    ...
    
    //construction isn't necessary, since it deserialize by the fields (thus transient will not be supported, unless indicated otherwise)
    ItemDefinition(String name, int id) {
    this.name = name;
    this.id = id;
    }
    
    }
    Say this is your current definitions, this is the methods you'll need to workout with:

    serialize
    Code:
           /*private static final*/Path PATH = Paths.get("data", "def", "items");
            File fileToSave = new File(PATH.normalize().toFile().getAbsoluteFile() + File.separator + fileName + ".json");
            if (!fileToSave.exists()) {
                try {
                    if (!fileToSave.createNewFile()) {
                        throw new IllegalStateException("Couldn't create file for item" + fileToSave.getName());
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try (FileWriter fw = new FileWriter(fileToSave)) {
                fw.write(GSON.toJson(STORE_HERE_WHATEVER_OBJECT)); //i.e ItemDefinitions.get(id)
                System.out.println("written to file " + mobDrop);
            } catch (Exception e) {
                e.printStackTrace();
            }

    deserialize
    Code:
    
           /*private static final*/Path PATH = Paths.get("data", "def", "items");
            File[] files = PATH.toFile().listFiles();
            if (files != null && files.length > 0) {
                for (File f : files) {
                    if (f == null || f.isDirectory()) continue;
                    try (FileReader reader = new FileReader(f)) {
                        ItemDefinition item= GSON.fromJson(reader, new TypeToken<ItemDefinition >() {
                        }.getType());
                        //item is deserialized (loaded),  you can now use it 
                        ItemDefinitions.put( item);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                throw new IllegalStateException("No Items loaded.");
            }

    If you'd like further and more proper explaination I'l'l gladly assist if you'd pm me your discord (or quote it)
    Thank you guys, completed it wasnt that hard.
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. txt player files convert to JSON
    By _Patrick_ in forum Requests
    Replies: 1
    Last Post: 03-27-2018, 09:35 AM
  2. CFG to Json Converter
    By Greg in forum Snippets
    Replies: 6
    Last Post: 01-30-2018, 01:45 PM
  3. Converting to Json format
    By CrazyPanda in forum Help
    Replies: 1
    Last Post: 02-25-2015, 05:13 PM
  4. Replies: 0
    Last Post: 01-12-2015, 03:05 AM
  5. [Asteria] spawn-config converter, to .json
    By CTucker in forum Tools
    Replies: 5
    Last Post: 06-14-2014, 01:38 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •