Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Float values are not serialized in NUI callback #2171

Closed
b2soft opened this issue Aug 25, 2023 · 4 comments
Closed

Float values are not serialized in NUI callback #2171

b2soft opened this issue Aug 25, 2023 · 4 comments
Assignees

Comments

@b2soft
Copy link

b2soft commented Aug 25, 2023

The Problem

I'm trying to use modern way of returning data from C# Client to NUI, utilizing result(object) call. For some reason float values are not serialized and equal null at NUI side.

Reproduce code

API.RegisterNuiCallback("Test::Request", new Action<IDictionary<string, object>, CallbackDelegate>(OnRequestData));
...
private void OnRequestData(IDictionary<string, object> arg, CallbackDelegate result)
{
    float vehicleSpeed = 42.0f;
    result(new { ok = true, speed = vehicleSpeed, test = "TEST" });
}

image

Possible problem

When I tested different types: float _a, double _b, int _c, long _d, string _e, bool _f, it seems like only float is not converted correctly
image

I observed, that value of this result is serialized first to JSON, and then to MessagePack.

static ResUICallback MakeUICallback(fx::Resource* resource, const std::string& type, const std::string& ref = {})

Possibly, JSON part serializes floats wrong.

Workaround

For now I have 2 workarounds:

  1. Use double everywhere instead of float
  2. Serialize object -> JSON first, myself in C# code, and then pass result string to callback. It works, since strings are sent OK
    result(JsonConvert.SerializeObject(new { ok = true, speed = vehicleSpeed }));
    image
@gottfriedleibniz
Copy link
Contributor

gottfriedleibniz commented Aug 25, 2023

There is a case of a missing case msgpack::type::FLOAT32: in ConvertToJSON.

@b2soft
Copy link
Author

b2soft commented Aug 28, 2023

There is a case of a missing case msgpack::type::FLOAT32: in ConvertToJSON.

Probably, that is the case. At least FLOAT32 and FLOAT may be encoded as Double. But we have only IsDouble, to keep from json conversion it's needed to add some float getter as well:
https://github.com/citizenfx/fivem/blob/master/code/client/shared/MsgpackJson.h#L36C26-L36C26

Float getter/setter were added only after this PR in rapidjson
Tencent/rapidjson#341

@gottfriedleibniz
Copy link
Contributor

Trying to round-trip float32's may lead some to backwards compatibility problems. Some care, if possible, will be required.

The missing msgpack::type::FLOAT32 in ConvertToJSON is a bug regardless.

@b2soft
Copy link
Author

b2soft commented Aug 30, 2023

I think FLOAT32/float can be treated as double and returned as double safely. It will not break any backward compatibility issues (because floats did not work) and would not affect new code, if you continue using doubles

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants