|
1 | | -# BlueNBT |
2 | | -BlueNBT is an NBT (De)Serializer with an API inspired by the [GSON](https://github.com/google/gson) library. |
3 | | -It's basically GSON for NBT instead of JSON data. |
4 | | -If you used GSON before you will feel right at home! |
5 | | - |
6 | | -## Requirements |
7 | | -BlueNBT requires **Java 21** |
8 | | - |
9 | | -## Adding BlueNBT to your Project |
10 | | - |
11 | | -### Gradle |
12 | | -```kotlin |
13 | | -repositories { |
14 | | - maven ( "https://repo.bluecolored.de/releases" ) |
15 | | -} |
16 | | - |
17 | | -dependencies { |
18 | | - implementation ( "de.bluecolored:bluenbt:3.0.3" ) |
19 | | -} |
20 | | -``` |
21 | | - |
22 | | -### Maven |
23 | | -```xml |
24 | | -<repositories> |
25 | | - <repository> |
26 | | - <id>bluecolored</id> |
27 | | - <url>https://repo.bluecolored.de/releases</url> |
28 | | - </repository> |
29 | | -</repositories> |
30 | | - |
31 | | -<dependency> |
32 | | - <groupId>de.bluecolored</groupId> |
33 | | - <artifactId>bluenbt</artifactId> |
34 | | - <version>3.0.3</version> |
35 | | -</dependency> |
36 | | -``` |
37 | | - |
38 | | -## Usage |
39 | | -**[API Javadoc](https://repo.bluecolored.de/javadoc/releases/de/bluecolored/bluenbt/BlueNBT/latest)** |
40 | | - |
41 | | -The primary class to use is [`BlueNBT`](https://github.com/BlueMap-Minecraft/BlueNBT/blob/master/src/main/java/de/bluecolored/bluenbt/BlueNBT.java). |
42 | | -You can easily create yourself an instance using `new BlueNBT()`. You can configure each BlueNBT instance separately with |
43 | | -different settings and Type(De)Serializers. You can reuse the same BlueNBT instance for as many (de)serialization operations |
44 | | -as you like. |
45 | | - |
46 | | -### First Example |
47 | | -First you want to declare the data-structure that you want to write/read to/from an NBT-Data stream. |
48 | | -For Example: |
49 | | -```java |
50 | | -class MyData { |
51 | | - int someNumber; |
52 | | - String someString; |
53 | | - long[] anArrayOfLongs; |
54 | | - List<MoreData> aLotMoreData; |
55 | | -} |
56 | | - |
57 | | -class MoreData { |
58 | | - boolean isData; |
59 | | - Map<String, List<String>> muchData; |
60 | | -} |
61 | | -``` |
62 | | -Now all you need to do to write all this data to an NBT-file is: |
63 | | -```java |
64 | | -BlueNBT blueNBT = new BlueNBT(); |
65 | | -try ( |
66 | | - OutputStream out = Files.newOutputStream(Path.of("myFile.nbt")); |
67 | | - OutputStream compressedOut = new BufferedOutputStream(new GzipOutputStream(out)) |
68 | | -){ |
69 | | - blueNBT.write(myData, compressedOut); |
70 | | -} |
71 | | -``` |
72 | | -And reading it again is equally easy: |
73 | | -```java |
74 | | -BlueNBT blueNBT = new BlueNBT(); |
75 | | -try ( |
76 | | - InputStream in = Files.newInputStream(Path.of("myFile.nbt")); |
77 | | - InputStream compressedIn = new BufferedInputStream(new GZIPInputStream(in)) |
78 | | -){ |
79 | | - MyData myData = blueNBT.read(compressedIn, MyData.class); |
80 | | -} |
81 | | -``` |
82 | | - |
83 | | -> [!NOTE] |
84 | | -> Both times we GZIP(De)Compressed our streams before writing/reading. This is because usually all nbt-files are |
85 | | -> GZIP-Compressed, BlueNBT does **not** do this for us to allow more flexibility. |
86 | | -> Also, make sure to use buffered streams before (de)compression to greatly improve compression-performance. |
87 | | -
|
88 | | -### Using TypeTokens |
89 | | -Sometimes you have a type with generic type-variables that you want to deserialize, for example `HashMap<String, Integer>`. |
90 | | -However, you can't easily create a `Class<?>` or `Type` for such a type that you can use to call `BlueNBT#read(InputStream in, Class<T> type)`. |
91 | | -This is where `TypeToken`s come in. |
92 | | -You can create a `TypeToken` in multiple ways: |
93 | | -```java |
94 | | -// simply from a Class<?> or Type |
95 | | -TypeToken.of(String.class) // String |
96 | | - |
97 | | -// for an array of a certain type |
98 | | -TypeToken.array(String.class) // String[] |
99 | | - |
100 | | -// for a raw Class<?> with additional type-parameters |
101 | | -TypeToken.of(Map.class, String.class, Integer.class) // Map<String, Integer> |
102 | | - |
103 | | -// or by creating a generic anonymous subclass of TypeToken |
104 | | -new TypeToken< Map<String, Collection<Integer>> >() {} // Map<String, Collection<Integer>> |
105 | | -``` |
106 | | -You can then pass this `TypeToken` to e.g. `BlueNBT#read(InputStream in, TypeToken<T> type)`. |
| 1 | +# BlueNBT |
| 2 | +BlueNBT is an NBT (De)Serializer with an API inspired by the [GSON](https://github.com/google/gson) library. |
| 3 | +It's basically GSON for NBT instead of JSON data. |
| 4 | +If you used GSON before you will feel right at home! |
| 5 | + |
| 6 | +## Requirements |
| 7 | +BlueNBT requires **Java 21** |
| 8 | + |
| 9 | +## Adding BlueNBT to your Project |
| 10 | + |
| 11 | +### Gradle |
| 12 | +```kotlin |
| 13 | +repositories { |
| 14 | + maven ( "https://repo.bluecolored.de/releases" ) |
| 15 | +} |
| 16 | + |
| 17 | +dependencies { |
| 18 | + implementation ( "de.bluecolored:bluenbt:3.0.3" ) |
| 19 | +} |
| 20 | +``` |
| 21 | + |
| 22 | +### Maven |
| 23 | +```xml |
| 24 | +<repositories> |
| 25 | + <repository> |
| 26 | + <id>bluecolored</id> |
| 27 | + <url>https://repo.bluecolored.de/releases</url> |
| 28 | + </repository> |
| 29 | +</repositories> |
| 30 | + |
| 31 | +<dependency> |
| 32 | + <groupId>de.bluecolored</groupId> |
| 33 | + <artifactId>bluenbt</artifactId> |
| 34 | + <version>3.0.3</version> |
| 35 | +</dependency> |
| 36 | +``` |
| 37 | + |
| 38 | +## Usage |
| 39 | +**[API Javadoc](https://repo.bluecolored.de/javadoc/releases/de/bluecolored/bluenbt/latest)** |
| 40 | + |
| 41 | +The primary class to use is [`BlueNBT`](https://github.com/BlueMap-Minecraft/BlueNBT/blob/master/src/main/java/de/bluecolored/bluenbt/BlueNBT.java). |
| 42 | +You can easily create yourself an instance using `new BlueNBT()`. You can configure each BlueNBT instance separately with |
| 43 | +different settings and Type(De)Serializers. You can reuse the same BlueNBT instance for as many (de)serialization operations |
| 44 | +as you like. |
| 45 | + |
| 46 | +### First Example |
| 47 | +First you want to declare the data-structure that you want to write/read to/from an NBT-Data stream. |
| 48 | +For Example: |
| 49 | +```java |
| 50 | +class MyData { |
| 51 | + int someNumber; |
| 52 | + String someString; |
| 53 | + long[] anArrayOfLongs; |
| 54 | + List<MoreData> aLotMoreData; |
| 55 | +} |
| 56 | + |
| 57 | +class MoreData { |
| 58 | + boolean isData; |
| 59 | + Map<String, List<String>> muchData; |
| 60 | +} |
| 61 | +``` |
| 62 | +Now all you need to do to write all this data to an NBT-file is: |
| 63 | +```java |
| 64 | +BlueNBT blueNBT = new BlueNBT(); |
| 65 | +try ( |
| 66 | + OutputStream out = Files.newOutputStream(Path.of("myFile.nbt")); |
| 67 | + OutputStream compressedOut = new BufferedOutputStream(new GzipOutputStream(out)) |
| 68 | +){ |
| 69 | + blueNBT.write(myData, compressedOut); |
| 70 | +} |
| 71 | +``` |
| 72 | +And reading it again is equally easy: |
| 73 | +```java |
| 74 | +BlueNBT blueNBT = new BlueNBT(); |
| 75 | +try ( |
| 76 | + InputStream in = Files.newInputStream(Path.of("myFile.nbt")); |
| 77 | + InputStream compressedIn = new BufferedInputStream(new GZIPInputStream(in)) |
| 78 | +){ |
| 79 | + MyData myData = blueNBT.read(compressedIn, MyData.class); |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +> [!NOTE] |
| 84 | +> Both times we GZIP(De)Compressed our streams before writing/reading. This is because usually all nbt-files are |
| 85 | +> GZIP-Compressed, BlueNBT does **not** do this for us to allow more flexibility. |
| 86 | +> Also, make sure to use buffered streams before (de)compression to greatly improve compression-performance. |
| 87 | +
|
| 88 | +### Using TypeTokens |
| 89 | +Sometimes you have a type with generic type-variables that you want to deserialize, for example `HashMap<String, Integer>`. |
| 90 | +However, you can't easily create a `Class<?>` or `Type` for such a type that you can use to call `BlueNBT#read(InputStream in, Class<T> type)`. |
| 91 | +This is where `TypeToken`s come in. |
| 92 | +You can create a `TypeToken` in multiple ways: |
| 93 | +```java |
| 94 | +// simply from a Class<?> or Type |
| 95 | +TypeToken.of(String.class) // String |
| 96 | + |
| 97 | +// for an array of a certain type |
| 98 | +TypeToken.array(String.class) // String[] |
| 99 | + |
| 100 | +// for a raw Class<?> with additional type-parameters |
| 101 | +TypeToken.of(Map.class, String.class, Integer.class) // Map<String, Integer> |
| 102 | + |
| 103 | +// or by creating a generic anonymous subclass of TypeToken |
| 104 | +new TypeToken< Map<String, Collection<Integer>> >() {} // Map<String, Collection<Integer>> |
| 105 | +``` |
| 106 | +You can then pass this `TypeToken` to e.g. `BlueNBT#read(InputStream in, TypeToken<T> type)`. |
0 commit comments