Skip to main content

juneau-examples-core

The juneau-examples-core module provides practical examples demonstrating how to use Apache Juneau's core marshalling capabilities for serialization and deserialization of Plain Old Java Objects (POJOs).

Overview

This module focuses on the fundamental serialization and deserialization features of Juneau Core, showing developers how to:

  • Convert POJOs to various content types (JSON, XML, RDF)
  • Parse content back into POJOs
  • Configure serializers and parsers with different properties
  • Handle complex object structures

Getting Started

To use the examples in this module, add the following dependency to your project:

<dependency>
<groupId>org.apache.juneau</groupId>
<artifactId>juneau-core</artifactId>
<version>${juneau.version}</version>
</dependency>

Example Categories

JSON Examples

Juneau provides comprehensive JSON support out of the box, allowing you to read and write JSON structures into POJOs with minimal configuration.

JsonSimpleExample.java

Demonstrates basic usage of JsonSerializer and JsonParser to convert POJOs to JSON strings and back to POJOs.

Key Features:

  • Simple POJO serialization to JSON
  • JSON parsing back to POJOs
  • Automatic type inference
  • Null handling

JsonConfigurationExample.java

Shows how to create JsonParser and JsonSerializer instances with custom configuration properties.

Key Features:

  • Custom serializer/parser configuration
  • Property customization
  • Formatting options
  • Validation settings

XML Examples

Juneau's XML support enables seamless conversion between POJOs and XML structures.

XmlComplexExample.java

Demonstrates how to use XmlSerializer and XmlParser to handle complex POJOs with nested objects and collections.

Key Features:

  • Complex object serialization to XML
  • Nested object handling
  • Collection serialization
  • XML namespace support

RDF Examples

For RDF support, Juneau integrates with Apache Jena to provide RDF serialization capabilities.

Prerequisites

Add Apache Jena to your classpath:

<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-core</artifactId>
<version>${jena.version}</version> <!-- Juneau is tested against 2.7.1 -->
</dependency>

RDF Support

RDF serialization support is available through the juneau-marshall-rdf module. While there isn't a dedicated example file, RDF functionality can be demonstrated using the existing serializers with RDF-specific configurations.

Key Features:

  • POJO to RDF conversion
  • Multiple RDF formats (RDF/XML, N3, NTriple, Turtle)
  • RDF vocabulary mapping
  • Semantic web integration
RDF Examples

RDF examples are integrated into the core serialization examples rather than having separate dedicated example files.

Running the Examples

All examples in this module are designed to be run as standalone Java applications. Each example class contains a main method that demonstrates the specific functionality.

Example Structure

public class ExampleClass {
public static void main(String[] args) {
// Example implementation
// Demonstrates specific Juneau functionality
}
}

Common Patterns

The examples follow these common patterns:

  1. Create POJOs - Define simple or complex Java objects
  2. Configure Serializers/Parsers - Set up Juneau components with desired properties
  3. Serialize - Convert POJOs to target format (JSON, XML, RDF)
  4. Parse - Convert formatted strings back to POJOs
  5. Validate - Ensure data integrity throughout the process

Best Practices

Performance Considerations

  • Reuse serializer/parser instances when possible
  • Configure appropriate buffer sizes for large objects
  • Use streaming for very large datasets

Error Handling

  • Always handle serialization/parsing exceptions
  • Validate input data before processing
  • Use appropriate logging for debugging

Configuration

  • Start with default configurations
  • Customize only when necessary
  • Document custom configurations

Integration with Other Modules

The examples in this module serve as the foundation for more complex scenarios found in:

juneau-examples-rest - REST API examples using these core conceptsjuneau-petstore - Complete application demonstrating end-to-end usagejuneau-microservice - Microservice examples building on core serialization Juneau Marshall Basics - Core marshalling conceptsJuneau Ecosystem Overview - Overall framework architecturejuneau-examples-rest - REST-specific examples

Source Code

The complete source code for all examples is available in the juneau-examples-core module of the Apache Juneau project.

Contributing

To contribute new examples or improve existing ones:

  1. Follow the established patterns in existing examples
  2. Include comprehensive comments and documentation
  3. Test examples with various data scenarios
  4. Update this documentation when adding new examples