Hone logo
Hone
Problems

JSON Data Processor in Rust

This challenge focuses on implementing basic JSON handling capabilities in Rust. You'll be tasked with parsing a JSON string into a Rust data structure, performing a simple transformation on the data, and then serializing the modified data back into a JSON string. This is a fundamental skill for any Rust developer working with APIs or data storage.

Problem Description

You are required to create a Rust program that reads a JSON string representing a list of products, transforms the prices of each product by applying a discount, and then outputs the modified data as a JSON string. The input JSON will contain an array of product objects. Each product object will have a "name" (String) and a "price" (f64) field. The discount should be 10%.

What needs to be achieved:

  1. Parse JSON: Parse the input JSON string into a Rust data structure. A serde_json::Value is a good starting point, but you'll likely want to define a struct for better type safety.
  2. Transform Data: Iterate through the parsed data, find the "price" field for each product, and apply a 10% discount. Update the "price" field with the discounted value.
  3. Serialize JSON: Serialize the modified data structure back into a JSON string.

Key Requirements:

  • Use the serde and serde_json crates for JSON serialization and deserialization. You'll need to add these to your Cargo.toml.
  • Handle potential errors gracefully during parsing and serialization. Return an appropriate error message if parsing fails.
  • The output JSON should be properly formatted and valid.
  • The discount calculation should be accurate (10% off).

Expected Behavior:

The program should take a JSON string as input, process it as described above, and output a JSON string representing the modified data. If the input JSON is invalid or an error occurs during processing, the program should print an error message to the console and exit with a non-zero exit code.

Edge Cases to Consider:

  • Invalid JSON: The input string might not be valid JSON.
  • Missing Fields: A product object might be missing the "name" or "price" field. Handle these cases gracefully (e.g., skip the product or log an error).
  • Non-Numeric Price: The "price" field might not be a valid number.
  • Empty Input: The input JSON might be an empty array.

Examples

Example 1:

Input: [
  {"name": "Laptop", "price": 1200.0},
  {"name": "Mouse", "price": 25.0},
  {"name": "Keyboard", "price": 75.0}
]
Output: [
  {"name": "Laptop", "price": 1080.0},
  {"name": "Mouse", "price": 22.5},
  {"name": "Keyboard", "price": 67.5}
]
Explanation: Each product's price is reduced by 10%.

Example 2:

Input: []
Output: []
Explanation: An empty array is returned as is.

Example 3: (Edge Case - Invalid JSON)

Input: "This is not JSON"
Output: Error: Invalid JSON format.
Explanation: The program should detect the invalid JSON and print an error message.

Constraints

  • Input Size: The input JSON string will be no longer than 10KB.
  • Data Types: Product names will be strings, and prices will be floating-point numbers (f64).
  • Error Handling: The program must handle JSON parsing errors and provide informative error messages.
  • Performance: The solution should be reasonably efficient for the given input size. Avoid unnecessary allocations or complex operations.

Notes

  • Consider defining a struct to represent a product for better type safety and code clarity.
  • The serde crate provides derive macros that can automatically generate serialization and deserialization code for your structs.
  • Use Result to handle potential errors during JSON parsing and serialization.
  • Think about how to handle missing or invalid fields in the input JSON. Should you skip the product, log an error, or return an error? Document your chosen approach.
  • Remember to add serde = { version = "1.0", features = ["derive"] } and serde_json = "1.0" to your Cargo.toml file.
Loading editor...
rust