Coder Social home page Coder Social logo

Comments (2)

EverNife avatar EverNife commented on August 25, 2024 2

This is not a bug.

You are creating an ArrayList that is not of <Strings>, but of <Object>!

List myWrongList = new ArrayList();
myWrongList.add(Arrays.asList("element1", "element2", "element3");
myWrongList.add("simpleString1");
myWrongList.add("simpleString2");
myWrongList.add("simpleString3");

Now, the myWrongList contains 4 elements, and not 6 elements.
myWrongList contains 1 List of Strings and 3 other strings

image

To fix your problem, rather than calling add(anotherList) you should be calling addAll

Code showing how to reproduce your problem.
	@Test
	void contextLoads() throws Exception {
		YamlFile firstYamlFile = new YamlFile(new File("teste.yml"));

		List<String> firstList = Arrays.asList(
				"OldElement1",
				"OldElement2",
				"OldElement3"
		);

		firstYamlFile.set("MyList", firstList);
		firstYamlFile.save();

		YamlFile secondYamlFile = new YamlFile(new File("teste.yml")); //Load from the previous value
		secondYamlFile.loadWithComments();

		List<String> retrievedList = secondYamlFile.getStringList("MyList");

		List mixedList = new ArrayList();
		mixedList.add(retrievedList);
		mixedList.add("newElement1");
		mixedList.add("newElement2");
		mixedList.add("newElement3");


		secondYamlFile.set("MyList2", mixedList);
		secondYamlFile.save();

		System.out.println(mixedList);
	}

from simple-yaml.

Carleslc avatar Carleslc commented on August 25, 2024

As stated, that is the intended behaviour. Thank you @EverNife for the answer.

With config.set("key", Arrays.asList(config.getStringList("key"), "newElement")) you're creating a list of lists instead of appending the previous elements. To append multiple elements you must use addAll.

Example to add elements to a list:

// Create the YamlFile
YamlFile config = new YamlFile("examples/test61.yml");

// Create a new config file if it does not exist or load its contents otherwise
config.createOrLoad();

// Set the list
config.set("key", new ArrayList<>(Arrays.asList("oldElement1", "oldElement2")));

// Get the previously set list (copy)
List<String> list = config.getStringList("key");

// Add several elements to the list
list.addAll(Arrays.asList("newElement1", "newElement2"));

// Add single element to the list
list.add("newElement3");

// Update the list elements
config.set("key", list);

// You can also add elements with list indexing!
config.set("key[-1]", "lastElement");

// Save the configuration file
config.save();
key:
  - oldElement1
  - oldElement2
  - newElement1
  - newElement2
  - newElement3
  - lastElement

from simple-yaml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.