← 返回 uber 的题目列表Implement a YAML Parser
类型:online_judge
Problem: Implement a Simplified YAML Parser
Implement a YAML parser that converts YAML text from standard input into a nested data structure and prints it as JSON.
Only the following YAML subset must be supported:
Spaces represent indentation; indentation is consistent at each nesting level.
Mappings (objects): key: value.
Nested mappings: key: followed by a more-indented block.
Lists: lines beginning with - .
Scalar values:
strings, optionally single- or double-quoted;
integers and floating-point numbers;
true / false;
null / ~.
Ignore blank lines and full-line comments beginning with #.
Full YAML features such as anchors, aliases, multiline strings, inline collections, complex keys, and tags are not required.
Input
Standard input contains multiple lines of YAML text.
Output
Print the parsed result as one valid JSON line. To make output deterministic, object keys must be sorted lexicographically and JSON separators must contain no unnecessary spaces.
Example 1
Input:
server:
host: localhost
port: 8080
Output:
{"server":{"host":"localhost","port":8080}}
Example 2
Input:
service:
enabled: true
replicas: 3
tags:
- backend
- production
Output:
{"service":{"enabled":true,"replicas":3,"tags":["backend","production"]}}
Constraints
Total input length is at most 100,000 characters.
There are at most 10,000 YAML lines.
Maximum nesting depth is at most 1,000.
Example
Input
server:
host: localhost
port: 8080
Output
{"server":{"host":"localhost","port":8080}}