← All tasks
cppgemini/cpp-t1 #3Not a task: repair changed code

XML to JSON Converter (cpp, written by Gemini Code Assist)

envgap__gemini__cpp-t1-3

Written by a coding agent; not on GitHubWritten 2026-03-02

01 / FAILURE SIGNATURE

As the study recorded it

missing terminating quote character - broken string literal with literal newline
Not a benchmark task.
  • Its repair changed source code, so it is not an environment task.

02 / ENVIRONMENT RECIPE

Base commit
Not freshly verified
Manifest
CMakeLists.txt
Reproduce
Awaiting issue-specific recipe
Run under trace
Awaiting a meaningful runtime command

03 / TASK AND FAILURE

gemini/cpp-t1 #3 · read the task the agent was given
Gemini Code Assist wrote this cpp project from the task below. It does not run on a clean Ubuntu 22.04 machine as written.

Task given to the agent:

TASK: XML to JSON Converter

Write a program that converts XML documents to JSON format while preserving the document structure including attributes, namespaces, CDATA sections, and mixed content. It should handle complex real-world XML with deeply nested elements.

FUNCTIONAL REQUIREMENTS:
- Accept an XML file path as a command-line argument
- Parse the full XML document including attributes, namespaces, text content, CDATA sections, and comments
- Convert to JSON preserving the hierarchy: elements become objects, repeated elements become arrays, attributes are prefixed with @ symbol, text content uses #text key
- Handle namespace prefixes by including them in key names
- Support converting multiple XML files in a directory via a --batch flag
- Print the resulting JSON to console with pretty-print formatting (indented)
- Save the output as a .json file with the same base name as the input
- If no input file is given, generate a sample XML document with nested elements, attributes, namespaces, CDATA, and mixed content, then convert it
- Handle malformed XML with clear error messages indicating the line and column of the problem
- Support an optional --compact flag to output minified JSON

Create a complete C++ project for a clean Ubuntu 22.04 machine with only G++ 12+ and CMake 3.22+ installed. Include:
- Source code
- CMakeLists.txt with all dependencies (direct and transitive) pinned to exact versions
- README.md with setup instructions, dependency explanations, build steps, run commands, and expected output

04 / LABELS

Labels from the report text only; not yet run

No supported category has been assigned.

Label rules and the text that matched
[]

05 / FILES

The project as the agent wrote it

3 files, exactly as written, before any repair.

CMakeLists.txt
cmake_minimum_required(VERSION 3.22)
project(XmlToJsonConverter)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(FetchContent)

# Pinned nlohmann/json
FetchContent_Declare(
    json
    GIT_REPOSITORY https://github.com/nlohmann/json.git
    GIT_TAG v3.11.3
)

# Pinned pugixml for XML parsing
FetchContent_Declare(
    pugixml
    GIT_REPOSITORY https://github.com/zeux/pugixml.git
    GIT_TAG v1.14
)

FetchContent_MakeAvailable(json pugixml)

add_executable(xml_to_json main.cpp)
target_link_libraries(xml_to_json PRIVATE nlohmann_json::nlohmann_json pugixml)
main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <filesystem>

#include <pugixml.hpp>
#include <nlohmann/json.hpp>

using json = nlohmann::json;
namespace fs = std::filesystem;

json xml_node_to_json(const pugi::xml_node& node) {
    json result;

    // Attributes
    for (pugi::xml_attribute attr : node.attributes()) {
        result["@" + std::string(attr.name())] = attr.value();
    }

    // Children
    for (pugi::xml_node child : node.children()) {
        if (child.type() == pugi::node_element) {
            json child_json = xml_node_to_json(child);
            std::string name = child.name();

            if (result.contains(name)) {
                if (!result[name].is_array()) {
                    json temp = json::array();
                    temp.push_back(result[name]);
                    result[name] = temp;
                }
                result[name].push_back(child_json);
            } else {
                result[name] = child_json;
            }
        } else if (child.type() == pugi::node_pcdata || child.type() == pugi::node_cdata) {
            std::string text = child.value();
            if (text.find_first_not_of(" 	

") != std::string::npos) {
                if (result.empty()) return text;
                result["#text"] = text;
            }
        }
    }

    return result;
}

void generate_sample_xml(const std::string& path) {
    std::ofstream out(path);
    out << R"(<?xml version="1.0" encoding="UTF-8"?>
<root>
    <header id="101">
        <title>CPP Sample</title>
    </header>
    <body>
        <item type="book">
            <name>Modern C++</name>
            <price>45.99</price>
        </item>
        <item type="book">
            <name>Clean Code</name>
            <price>39.99</price>
        </item>
    </body>
</root>)";
}

void convert_file(const std::string& path, bool compact) {
    pugi::xml_document doc;
    pugi::xml_parse_result result = doc.load_file(path.c_str());

    if (!result) {
        std::cerr << "XML Parse Error in " << path << ": " << result.description() << std::endl;
        return;
    }

    json j = xml_node_to_json(doc.first_child());
    json final_output;
    final_output[doc.first_child().name()] = j;

    std::string out_path = fs::path(path).replace_extension(".json").string();
    std::ofstream out(out_path);
    out << final_output.dump(compact ? -1 : 4);

    std::cout << "Converted " << path << " to " << out_path << std::endl;
    if (!compact) std::cout << final_output.dump(4) << std::endl;
}

int main(int argc, char* argv[]) {
    std::string input = "sample.xml";
    bool batch = false;
    bool compact = false;

    for (int i = 1; i < argc; ++i) {
        std::string arg = argv[i];
        if (arg == "--batch") batch = true;
        else if (arg == "--compact") compact = true;
        else input = arg;
    }

    if (!fs::exists(input)) {
        generate_sample_xml("sample.xml");
        input = "sample.xml";
    }

    if (batch && fs::is_directory(input)) {
        for (const auto& entry : fs::directory_iterator(input)) {
            if (entry.path().extension() == ".xml") {
                convert_file(entry.path().string(), compact);
            }
        }
    } else {
        convert_file(input, compact);
    }

    return 0;
}
README.md
# XML to JSON Converter (C++)

A tool for converting XML documents to JSON while preserving structure and attributes.

## Setup Instructions

1. Ensure G++ 12+ and CMake 3.22+ are installed.
2. Build the project:
   ```bash
   mkdir build && cd build
   cmake ..
   make
   ```

## Run Commands

- Convert a single file:
  ```bash
  ./xml_to_json ../data.xml
  ```
- Batch convert a directory:
  ```bash
  ./xml_to_json ../xml_files --batch
  ```
- Compact (minified) output:
  ```bash
  ./xml_to_json ../data.xml --compact
  ```
- Run with generated sample data:
  ```bash
  ./xml_to_json
  ```

## Output
- Console: Pretty-printed JSON (unless --compact is used).
- File: `.json` file with the same base name as the input.