From 7fc9d98ff31d11183834db9cc53aad01263f45b9 Mon Sep 17 00:00:00 2001 From: dankmeme01 <42031238+dankmeme01@users.noreply.github.com> Date: Thu, 14 Nov 2024 09:36:20 +0100 Subject: [PATCH] add parseAs --- include/matjson.hpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/include/matjson.hpp b/include/matjson.hpp index fa15979..1e28968 100644 --- a/include/matjson.hpp +++ b/include/matjson.hpp @@ -143,6 +143,19 @@ namespace matjson { /// @return The parsed JSON value or an error static geode::Result parse(std::string_view source); + /// Parses JSON from a string and tries to convert it to a given type + /// @param source The JSON string to parse + /// @return The parsed struct or an error + template + inline static geode::Result parseAs(std::string_view source) { + auto parsed = Value::parse(source); + if (!parsed) { + return geode::Err(static_cast(parsed.unwrapErr())); + } + + return Serialize::fromJson(parsed.unwrap()); + } + /// Parses JSON from an input stream /// @param source Stream to parse /// @return The parsed JSON value or an error @@ -359,6 +372,15 @@ namespace matjson { return Value::parse(source); } + /// Parses JSON from a string and tries to convert it to a given type + /// @param source The JSON string to parse + /// @return The parsed struct or an error + /// @note Shorthand for Value::parse + template + inline geode::Result parseAs(std::string_view source) { + return Value::parseAs(source); + } + /// Parses JSON from an input stream /// @param source Stream to parse /// @return The parsed JSON value or an error