Comments (6)
Glad to see you were able to address the issue.
This is a side-effect of using getList
method, which returns the internal list as is (the same happens with the raw get
method). In case it is a List or Map then mutating the object will mutate the internal list, which will be reflected in the yml file on save.
I suggest you to use getStringList
instead, which returns a copy of the internal list, already casted to List<String>
. That copy can be mutated without affecting the yml file.
So, your issue would be resolved changing
List<String> answers = (List<String>) DataYML.getYML().getList("test.answers");
to
List<String> answers = DataYML.getYML().getStringList("test.answers");
I guess DataYML.getYML()
is either a YamlFile
or YamlConfiguration
instance.
Casting the getList
to (List<String>)
directly can lead to such side-effects. If you want to use getList
and mutate the list without affecting the yml state then you should first get a copy of the list using new ArrayList<String>((List<String>) DataYML.getYML().getList("test.answers"))
. But I still suggest you to use getStringList
which handles the copy ensuring items are actually strings, something it is not handled by casting to (List<String>)
directly so it could lead to other unexpected behaviour. getList
method is intended to be used when you do not know the type of the items, or they are of different types.
Another suggestion, apart from the issue: you can use Collections.shuffle(answers)
method from the Java API to rearrange the list randomly (just make sure you are using getStringList
or the new random order will be reflected to the yml file as well).
from simple-yaml.
That's awesome to know! Thank you for letting me know about them, and I guess I shouldn't use .get
since it would have the same effect. Very helpful!
from simple-yaml.
It may be something related to flow-style lists. Have you tried using a sequence with indentation?
questions:
'1':
answers:
- Sojourner
- Spirit
- Opportunity
- Curiosity
- Perseverance
from simple-yaml.
It may be something related to flow-style lists. Have you tried using a sequence with indentation?
questions: '1': answers: - Sojourner - Spirit - Opportunity - Curiosity - Perseverance
I have. In fact, I tried that first and then switched to what I'm using now thinking it would solve the problem, and it didn't. Even though I dont set or change anything in the answers, I think my code later may be a problem, because if you do steps 1-5 it's completely fine, but for some reason, the later code is what makes the difference. I'll reply with it once I can, and please excuse how inefficient it may be.
from simple-yaml.
Do you get the non-empty list of answers in your code when loading the file and reading answers?
Provide the relevant parts of your code to reproduce the situation, where you load the yaml, read values, change questions.1.used
and save the yaml again (is with the save where the yml file changes).
from simple-yaml.
I found out the issue. I'm sorry if I wasted your time due to my lack of experience, but here it is:
I experimented with my code and found what's causing it. Here's data.yml for the experiment:
Nothing new, now here's the code:
Basically, get the answers, store them in a list, then use some code to rearrange them randomly (because the first answer is the correct one). But I made one mistake that I didn't realize, and it's that I removed the answers from the list when rearranging them (using answers.remove()
), and this caused them to be removed from data.yml itself, which is new to me and mainly why I was confused because I thought it would only remove it from the variable answers
.
(The experiment is that I used different code without answers.remove()
and it worked fine)
from simple-yaml.
Related Issues (20)
- Load YamlConfiguration Error HOT 1
- Key setting with single quote despite no special characters. HOT 10
- Header being duplicated when there is no comment on the root key HOT 3
- There is a bug when i set the list HOT 2
- NoSuchMethodError when registering config HOT 6
- Invalid configuration caused by the library itself? HOT 3
- addDefault doesn't work since 1.8 if file contains only defaults HOT 5
- Enhancement/Feature Request - addDefault() enforce quote style HOT 3
- Bump up of SnakeYML to 1.31. HOT 1
- (Velocity dependency clash) java.lang.NoSuchMethodError HOT 13
- java.lang.NoSuchMethodError: 'void org.yaml.snakeyaml.DumperOptions.setIndentWithIndicator(boolean)' HOT 3
- Could not find artifact me.carleslc.Simple-YAML:Simple-Configuration:jar:1.8.3 in central (https://repo1.maven.org/maven2) HOT 6
- [Question] String length limit! Can it be disabled? HOT 3
- Update snakeyaml version to 2.0 HOT 1
- The method for saving the config is not working. HOT 2
- ConfigurationSection#addDefault changes values if present HOT 2
- Custom Domain Jitpack Deployment HOT 5
- String List placement inside yaml HOT 10
- YamlFile#load & #loadWithComments behave different on double quote value HOT 6
- Memory Leak Problem HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from simple-yaml.