Ok, maybe it's not exactly a Codable issue.
I am working on a websocket server. The server receives a message in Json:
{"auth":{"username":"byname","password":"pass"}}
The server itself reads the message and only cares about the "auth" key. It uses that key to route the message to the appropriate module. In this case the auth module.
The message is then routed to the auth module, who will try to decode the message into a struct.
I am thinking on doing a protocol Message. The protocol will have a variable named "raw" which will be a String. I will implement an extension on message, to read the routing key (auth).
I need to decode the message without parsing to an object yet. Because the server does not know what the module model looks like. So it passes a message which has a raw.
Now the module when it gets the message, it wants to parse it to its own model. In this case: username, password. If I apply Codable at this moment the message will have to have this structure: auth = ["username":"xxx","password":xxx"].. I will like to only have a struct with username and password not with auth nested..
How will you remove the auth key and extract the value without knowing that kind of values it holds? ANY does not seem to work with Codable. With manual JSON parsing does work.. But my approach seems cumbersome.
I tried removing the key by hand by inspecting the String, but it has to account for a lot of edge cases where it might fail by LF, TABS, etc....
My only solution seems to convert from String JSON to a dictionary of [String:Any] and then extracting the Any part and coding it back to JSON, and then decode it again in the actual model struct.
My brain is blowing . I am a newbie and I hope someone could shine some light on what would be the best approach to do it.
In summary. I want to inspect a JSON string to return the first key. Then I want to remove that key or get the child objects from the key, and pass it to another func which will in turn use Codable to parse the string into the model. My question is what would be the most efficient and clean way in your POV of doing this
Thanks a lot for the help in advance