You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is the [JSON](https://www.json.org/json-en.html) format, a human-readable data-interchange format that transmits data objects consisting of key-value pairs and arrays.
66
67
67
-
In Rust, there is a popular crate available for taking some data and converting it into or "**serializing**" it in the JSON format. We can use the [`serde_json`](https://docs.rs/serde_json/latest/serde_json/) crate for this.
68
+
In Rust, there is a popular crate available for taking some data and converting it (or "**serializing**" it) into the JSON format.
69
+
We can use the [`serde_json`](https://docs.rs/serde_json/latest/serde_json/) crate for this.
68
70
69
-
Let's add this to our Cargo.toml file. We'll need to bring in the `derive` feature from `serde` so that we can derive the `Serialize` implementation for our structs. We'll also need to bring `serde_json` so that we can convert our struct to a JSON formatted `String`.
71
+
Let's add this to our Cargo.toml file.
72
+
We'll need to bring in the `derive` feature from `serde` so that we can derive the `Serialize` implementation for our structs.
73
+
We'll also need to bring `serde_json` so that we can convert our struct to a JSON formatted `String`.
70
74
71
75
```toml
72
76
[package]
@@ -84,9 +88,13 @@ serde_json = "1.0.115"
84
88
85
89
We can follow the example from the documentation here: https://docs.rs/serde_json/latest/serde_json/index.html#creating-json-by-serializing-data-structures.
86
90
87
-
First, we'll have to import the `Serialize` feature with the `use serde::Serialize;` statement. Then, we can add the `#[derive(Serialize)]` attribute to our `Input` struct. This will work as long as all of the primitive types it contains can be serialized. Finally, we'll call `to_string()` method on `serde_json` to convert inputs into a JSON formatted string. Let's try that and see what happens.
91
+
First, we'll have to import the `Serialize` feature with the `use serde::Serialize;` statement.
92
+
Then, we can add the `#[derive(Serialize)]` attribute to our `Input` struct.
93
+
This will work as long as all of the primitive types it contains can be serialized.
94
+
Finally, we'll call `to_string()` method on `serde_json` to convert inputs into a JSON formatted string.
95
+
Let's try that and see what happens.
88
96
89
-
*Reminder: We don't need to import top-level modules such as `serde_json` as those will already be included by default in our `main.rs` file.*
97
+
*Note: We don't need to import top-level modules such as `serde_json` as those will already be included by default in our `main.rs` file.*
Our `txid` and `script` fields need to be converted to hexadecimal format so that they appear more human readable. For now, we can store them as `String` types instead. This way we can leverage the `hex` library we've already included, and call `hex::encode` to encode both of these types as hex strings.
148
+
Our `txid` and `script` fields need to be converted to hexadecimal format so that they appear more human readable.
149
+
For now, we can store them as `String` types instead.
150
+
This way we can leverage the `hex` library we've already included, and call `hex::encode` to encode both of these types as hex strings.
142
151
143
-
*Note: It's probably a better idea to store these as their original types for internal purposes and calculations and separate the logic for how we display or serialize data from how it is stored. We'll revisit this separation of concerns in lesson 16 and talk about how to do that.*
152
+
*Note: It's probably a better idea to store these as their original types for internal purposes and calculations and separate the logic for how we display or serialize data from how it is stored.
153
+
We'll revisit this separation of concerns in lesson 16 and talk about how to do that.*
144
154
145
155
First, we'll update our `Input` string to change both of these fields to `String` types:
146
156
```rust
@@ -175,34 +185,36 @@ Let's run this now and see what happens.
Ok, a little better but it's still hard to read. Let's look at the documentation and see if there's a better method we can use: https://docs.rs/serde_json/latest/serde_json/#functions. Looks like there's a method called `to_string_pretty`. Let's try that instead of `to_string` and see how that looks.
191
+
Ok, a little better but it's still hard to read.
192
+
Let's look at the documentation and see if there's a better method we can use: https://docs.rs/serde_json/latest/serde_json/#functions.
193
+
Looks like there's a method called `to_string_pretty`.
194
+
Let's try that instead of `to_string` and see how that looks.
Ok, that's way better! Starting to look much more similar to the output from `bitcoin-cli`.
202
215
203
-
Let's make one more modification to place all the different pieces of a transaction, such as the version, inputs and outputs all into one `Transaction` struct.
204
-
205
-
We'll add the `Transaction` struct:
216
+
Let's make one more modification to place all the different pieces of a transaction, such as the version, inputs and outputs all into a `Transaction` struct.
217
+
First, declare the `Transaction` struct:
206
218
```rust
207
219
#[derive(Debug, Serialize)]
208
220
structTransaction {
@@ -244,7 +256,7 @@ fn main() {
244
256
}
245
257
```
246
258
247
-
And now we should see an output with everything neatly printed out under one `Transaction` JSON object!
259
+
Now we should see an output with everything neatly printed out under one `Transaction` JSON object!
248
260
249
261
```console
250
262
Transaction: {
@@ -266,7 +278,9 @@ Transaction: {
266
278
}
267
279
```
268
280
269
-
Pretty neat! Let's keep moving and finish decoding the transaction in the next lesson. After that, we'll set up our program to handle user inputs and deal with handling errors. Onwards!
281
+
Pretty neat! Let's keep moving and finish decoding the transaction in the next lesson.
282
+
After that, we'll set up our program to handle user inputs and deal with handling errors.
0 commit comments