#Deserializing from a string to a struct

20 messages · Page 1 of 1 (latest)

cursive mirage
#

When trying to deserialize this string: json {"verification_token": "c2e82f6c-45b1-459d-aad7-d835fdf7430b", "message_id": "dd7ef772-4778-4a2f-bad5-947857f25cf9", "timestamp": "2022-11-18T09:07:20Z", "type": "Donation", "is_public": true, "from_name": "Ko-fi Team", "message": "Good luck with the integration!", "amount": "3.00", "url": "https://ko-fi.com/Home/CoffeeShop?txid=00000000-1111-2222-3333-444444444444", "email": "[email protected]", "currency": "USD", "is_subscription_payment": false, "is_first_subscription_payment": false, "kofi_transaction_id": "00000000-1111-2222-3333-444444444444", "shop_items": null, "tier_name": null, "shipping": null}

into this struct:

#[derive(serde::Deserialize, serde::Serialize, Debug)]
struct Payment {
    verification_token: String,
    message_id: String,
    timestamp: String,
    #[serde(rename = "type")]
    type_: String,
    is_public: bool,
    from_name: String,
    message: String,
    amount: f64,
    url: String,
    email: String,
    currency: String,
    is_subscription_payment: bool,
    is_first_subscription_payment: bool,
    kofi_transaction_id: String,
}

I get the error:

Error("expected value", line: 1, column: 23)

I googled for this issue and found that this can be the case when the value in the JSON string can't be parsed as the type in the struct, but surely it should be ok to be parsed as a string?

knotty wyvern
#

by doing that, I get this rs #[derive(Serialize, Deserialize)] struct Struct1 { amount: String, currency: String, email: String, from_name: String, is_first_subscription_payment: bool, is_public: bool, is_subscription_payment: bool, kofi_transaction_id: String, message: String, message_id: String, shipping: (), shop_items: (), tier_name: (), timestamp: String, r#type: String, url: String, verification_token: String, }

#

maybe those null ones that are () on the rust side should be Option?

#

so maybe just take that struct and start experimenting to make it fit your use cases while keeping it working

#

ok I sorted them to the same order as the json rs #[derive(Serialize, Deserialize)] struct Struct1 { verification_token: String, message_id: String, timestamp: String, r#type: String, is_public: bool, from_name: String, message: String, amount: String, url: String, email: String, currency: String, is_subscription_payment: bool, is_first_subscription_payment: bool, kofi_transaction_id: String, shop_items: (), tier_name: (), shipping: (), }

knotty wyvern
#

so it just cant parse verification_token for some reason

cursive mirage
#

I get the same error if I try to deserialize into HashMap<String, String> too

tacit cove
cursive mirage
#

Oh my god the JSON had invisible unicode characters

#

Which Discord stripped out when I copy-pasted it here

#

😭

knotty wyvern
#

omg

#

?eval rs let test1 = 1​0; let test2 = 4;

grizzled rapidsBOT
#
error: unknown start of token: \u{200b}
 --> src/main.rs:2:14
  |
2 | let test1 = 10;
  |              ^

error: unknown start of token: \u{37e}
 --> src/main.rs:3:14
  |
3 | let test2 = 4;
  |              ^
  |
help: Unicode character ';' (Greek Question Mark) looks like ';' (Semicolon), but it is not
  |
3 | let test2 = 4;
  |              ~

error: expected one of `.`, `;`, `?`, `else`, or an operator, found `0`
 --> src/main.rs:2:15
  |
2 | let test1 = 10;
  |              ^ expected one of `.`, `;`, `?`, `else`, or an operator
knotty wyvern
#

huh I expected it to call out zero width space better

#

thats still much better than "expected value"