# Change the state of instancied class by a method with json file

**URL:** https://forum.crystal-lang.org/t/change-the-state-of-instancied-class-by-a-method-with-json-file/7003
**Category:** Help & Support
**Created:** [July 8, 2024, 6:39pm UTC](https://forum.crystal-lang.org/t/change-the-state-of-instancied-class-by-a-method-with-json-file/7003 "2024-07-08T18:39:04Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Fulgurance](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fulgurance/32/1463_2.png) [@Fulgurance](https://forum.crystal-lang.org/u/Fulgurance)
#### Post date: [July 8, 2024, 6:39pm UTC](https://forum.crystal-lang.org/t/change-the-state-of-instancied-class-by-a-method-with-json-file/7003/1 "2024-07-08T18:39:04Z")

</div>

Hi guys, actually I am updating my code to do a proper json file support. I am actually coding a class method to load a class from a json file AND an instance method as well to load value from json file.

The problem is, I don’t know how to implement that properly for the instance method. I am getting always a compilation error.

How can I perform that ?

The code:

```auto
module ISM

    class FavouriteGroup

        def_clone

        include JSON::Serializable

        property name : String
        property softwares : Array(String)

        def initialize(@name = ISM::Default::FavouriteGroup::Name, @softwares = Array(String).new)
        end

        def self.filePath(name = ISM::Default::FavouriteGroup::Name) : String
            return Ism.settings.rootPath+ISM::Default::Path::FavouriteGroupsDirectory+name+".json"
        end

        def self.generateConfiguration(path = filePath)
            file = File.open(path,"w")
            self.new.to_json
            file.close
        end

        def self.loadConfiguration(path = filePath)
            if !File.exists?(path)
                generateConfiguration(path)
            end

            return from_json(File.read(path))
        end

        def loadConfiguration(path = self.class.filePath)
            if !File.exists?(path)
                writeConfiguration(path)
            end

            self = self.class.from_json(File.read(path))
        end

        def writeConfiguration(path = self.class.filePath)
            file = File.open(path,"w")
            to_json(file)
            file.close
        end

    end

end

```

As I expected, we cannot do that:

```auto
self = self.class.from_json(File.read(path))

```

But how can I perform that ?

---

<div class="post-metadata">

### Author: ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)
#### Post date: [July 8, 2024, 9:55pm UTC](https://forum.crystal-lang.org/t/change-the-state-of-instancied-class-by-a-method-with-json-file/7003/2 "2024-07-08T21:55:27Z")

</div>

To start, it feels a bit wrong to create a new instance, just to save it to a file as part of the `.generateConfiguration` method. Like at that point just do like `FavouriteGroup.new.save` or something. Also it has a bug in that you’re not passing `file` to `#to_json`, so it’s not saving anything.

> [@Fulgurance](#):
>
> I am actually coding a class method to load a class from a json file AND an instance method as well to load value from json file.

What’s actually the use case here? Is there an expectation that the JSON file may be edited outside of this `FavouriteGroup` type causing it to become out of sync or?

---

<div class="post-metadata">

### Author: ![Fulgurance](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fulgurance/32/1463_2.png) [@Fulgurance](https://forum.crystal-lang.org/u/Fulgurance)
#### Post date: [July 9, 2024, 11:32am UTC](https://forum.crystal-lang.org/t/change-the-state-of-instancied-class-by-a-method-with-json-file/7003/3 "2024-07-09T11:32:31Z")

</div>

I will update that. To be honest I don’t know why I done that 😐

Thank you mate
