in Technology by
How to use YAML files in high programming languages such as JAVA, Python, etc?

1 Answer

0 votes
by

YAML is supported in most programming languages and can be easily integrated with user programs.
In JAVA we can use the Jackson module which also parses XML and JSON. For e.g

// We need to declare Topic class with necessary attributes such as name, total_score, user_score, sub_topics
List<Topic> topics = new ArrayList<Topic>();
topics.add(new Topic("String Manipulation", 10, 6));
topics.add(new Topic("Knapsack", 5, 5));
topics.add(new Topic("Sorting", 20, 13));
// We want to save this Topic in a YAML file
Topic topic = new Topic("DS & Algo", 35, 24, topics);
// ObjectMapper is instantiated just like before
ObjectMapper om = new ObjectMapper(new YAMLFactory());
// We write the `topic` into `topic.yaml`
om.writeValue(new File("/src/main/resources/topics.yaml"), topic);
---
name: "DS & Algo"
total_score: 35
user_score: 24
sub_topics:
- name: "String Manipulation"
 total_score: 10
 user_score: 6
- name: "Knapsack"
 total_score: 5
 user_score: 5
- name: "Sorting"
 total_score: 20
 user_score: 13

Similarly, we can read from YAML also:

// Loading the YAML file from the /resources folder
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
File file = new File(classLoader.getResource("topic.yaml").getFile());
// Instantiating a new ObjectMapper as a YAMLFactory
ObjectMapper om = new ObjectMapper(new YAMLFactory());
// Mapping the employee from the YAML file to the Employee class
Topic topic = om.readValue(file, Topic.class);

In python similarly, we can use the pyyaml library and read and write easily in YAML format.

Related questions

0 votes
    which two languages contributed to Python as a programming language Select the correct answer from above options...
asked Dec 14, 2021 in Education by JackTerrance
0 votes
    which of the following tab should be used to add and delete element such as chapter number ,hyperlink etc Select the correct answer from above options...
asked Nov 27, 2021 in Education by JackTerrance
0 votes
    I need to use environment variable "PATH" in yaml file which needs to be parsed with a script. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    Explain how principles of computer programming are applied in different languages to produce software applications. Select the correct answer from above options...
asked Dec 9, 2021 in Education by JackTerrance
0 votes
    Explain how principles of computer programming are applied in different languages to produce software applications. Select the correct answer from above options...
asked Dec 3, 2021 in Education by JackTerrance
0 votes
    Smart Contracts in R3 Corda are written using programming languages like ______ and _____. A. Go , C++ B. Java, Python C. Solidity, Vyper D. Java, Kotlin...
asked Feb 23, 2023 in Technology by JackTerrance
0 votes
    App Engine Standard Environment supports which of the following programming languages? (1)PHP (2)Python (3)All the options (4)Go (5)Java...
asked Sep 1, 2021 in Technology by JackTerrance
0 votes
    DAST is independent of programming languages. 1.False 2. True...
asked Oct 28, 2020 in Technology by JackTerrance
0 votes
    Can a static library (.a) contain resources like images, sound files etc?...
asked Nov 10, 2020 in Technology by JackTerrance
0 votes
    We have written clients in different programming languages (Java, .NET/Silverlight, Flash, Javascript) that ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 9, 2022 in Education by JackTerrance
0 votes
    We have written clients in different programming languages (Java, .NET/Silverlight, Flash, Javascript) that ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    Explain about subprogrammes in principles of programming languages Select the correct answer from above options...
asked Dec 20, 2021 in Education by JackTerrance
0 votes
    Applications developed by programming languages like ____ and ______ have this common buffer-overflow error. (a) C, Ruby ... questions and answers pdf, mcq on Cyber Security pdf,...
asked Nov 4, 2021 in Education by JackTerrance
0 votes
    _______________ is a procedural extension of Oracle - SQL that offers language constructs similar to those in ... in chapter Query Processing Techniques of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    What programming languages are used by Cucumber?...
asked Jul 20, 2021 in Education by JackTerrance
...