How to construct Parse Objects from JSONs while maintaining createdAt values?

I have JSONs like this;

{
    createdAt: a_date,
    text: "some text",
    likes: 8
}

And I want to convert them into PFObjects… here’s how I do it;

let post = PFObject(className: "Post", dictionary: jsonPost)

The thing is that after that, the PFObject doesn’t have a value at its createdAt property.
post.createdAt <== this is always nil

Any idea why?

I am not sure, but you have trying by passing _created_at instead?

I just tried that and it crashed again

I believe you need to pass { __type: “Date”, iso: “the date” } instead of the Date directly

Still nothing… you meant like this, right?

post["_created_at"] = ["__type": "Date", "iso": "2020-09-23T13:31:08.877Z"]
return PFObject(className: "Post", dictionary: post)

Yes. Have you tried both _created_at and createdAt?

Yes I have and it still remains nil

Also I found out that the objectId property is also nil. I tried this, but didn’t do anything;

post["_id"] = post["objectId"] as? String

It seems the key’s you are trying to set, createdAt, objectId, are protected keys. These are usually only hydrated from the parse-server.

You can see how encoding of pare objects occurs here: https://github.com/parse-community/Parse-SDK-iOS-OSX/blob/5bf27a2d173d822c8e8d955ca9f25efccd7c0787/Parse/Parse/Internal/Object/State/PFObjectState.m#L110-L137

This test case may help you figure out how to solve your problem, https://github.com/parse-community/Parse-SDK-iOS-OSX/blob/5bf27a2d173d822c8e8d955ca9f25efccd7c0787/Parse/Tests/Unit/ObjectStateTests.m#L180-L192