1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Jackson3におけるtoml・jsonの読み書き

Last updated at Posted at 2025-10-13

はじめに

Jacksonとは、JSON(や、その他のテキスト形式)を読み書きできる。

pom

ポイント

  • groupId変更に注意(tools.jackson)
  • jackson-bomが利用可能
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>jackson-toml</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>jackson-toml</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.junit</groupId>
        <artifactId>junit-bom</artifactId>
        <version>5.14.0</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>tools.jackson</groupId>
        <artifactId>jackson-bom</artifactId>
        <version>3.0.0</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.platform</groupId>
      <artifactId>junit-platform-launcher</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <scope>test</scope>
    </dependency>
    <!--  Jackson  -->
    <dependency>
      <groupId>tools.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
    </dependency>
    <dependency>
      <groupId>tools.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
    </dependency>
    <dependency>
      <groupId>tools.jackson.dataformat</groupId>
      <artifactId>jackson-dataformat-toml</artifactId>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.5.4</version>
      </plugin>
      <plugin>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>3.5.4</version>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>21</source>
          <target>21</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

実装&テスト

今回は簡単にするため、JUnitで動作確認を行う

package org.example;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.parallel.ExecutionMode.SAME_THREAD;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.params.ParameterizedClass;
import org.junit.jupiter.params.provider.MethodSource;
import tools.jackson.core.json.JsonWriteFeature;
import tools.jackson.databind.SerializationFeature;
import tools.jackson.databind.json.JsonMapper;
import tools.jackson.dataformat.toml.TomlMapper;
import tools.jackson.dataformat.toml.TomlReadFeature;

import java.util.stream.Stream;

@Execution(SAME_THREAD)
@ParameterizedClass
@MethodSource("data")
record AppTest(String tomlString, String jsonString) {
    static Stream<String[]> data() {
        String[] s1 = new String[]{"""
foo = 'bar'
[nested]
baz = 4
""", """
{
  "foo" : "bar",
  "nested" : {
    "baz" : 4
  }
}"""};
        return Stream.of(s1, s1).limit(1);
    }

    @Test
    void tomlMapperTest() {
        TestRecord expected = new TestRecord("bar", new TestRecord.Nested(4));
        TestRecord toml = readToml(tomlString);
        assertEquals(expected, toml);
        assertEquals(jsonString, writeJson(toml));
    }

    private TestRecord readToml(String input){
        TomlMapper mapper = TomlMapper.builder()
                .enable(TomlReadFeature.PARSE_JAVA_TIME)
                .build();
        return mapper.readValue(input, TestRecord.class);
    };

    private String writeJson(TestRecord input){
        JsonMapper mapper = JsonMapper.builder()
                .enable(JsonWriteFeature.QUOTE_PROPERTY_NAMES)
                .enable(SerializationFeature.INDENT_OUTPUT)
                .build();
        return mapper.writeValueAsString(input).replace("\r\n", "\n");
    };

    public record TestRecord(String foo, Nested nested){
        public record Nested(int baz){};
    };
}

結果出力

image.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?