Can't select object properties to serialize
See original GitHub issueI have data in my Mongo DB that looks like this:
[
{
role: 'admin',
local: {
password: '___password_hash___',
email: 'admin@example.com'
}
},
{
role: 'standard',
local: {
password: '___password_hash___',
email: 'standard@example.com'
}
}
]
I want to write a serializer that excludes the password
property (for obvious reasons), so that each user
object would look like this:
{
role: 'admin',
local: {
email: 'admin@example.com'
}
}
I’ve tried this as a serializer:
const JSONAPISerializer = require('jsonapi-serializer').Serializer;
module.exports = new JSONAPISerializer('user', {
attributes: ['role', 'local'],
local: {
attributes: ['email']
},
pluralizeType: false,
keyForAttribute: 'camelCase'
});
My understanding is that the attributes: ['email']
line specifies the list of attributes in the local
object that should be returned – and that no other attributes will be returned.
However, even using that serializer, I still get this returned:
{
"data": [
{
"type": "user",
"id": "587e6c7a62af4891d801b0c8",
"attributes": {
"role": "admin",
"local": {
"email": "admin@example.com",
"password": "___password_hash___"
}
}
},
{
...
}
]
Help! What should I be doing? I must be missing something small.
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Unable to Json.Serialize an array of objects inside a ViewModel
var xyz = @Json.Serialize(Model.Select(x => x.PropName).ToArray());. However, I have a model where an object array is ...
Read more >Serialize C# Properties (how-to with code) - Unity Forum
Hi all! There's one particular C# feature I use a lot in my projects, and that is properties: They allow you to execute...
Read more >4 ways to make a property deserializable but not serializable ...
Approach 1: Use attributes JsonProperty together with JsonIgnore Below is the sample code: · Approach 2: Add ShouldSerialize method. Newtonsoft.
Read more >How to serialize properties of derived classes with System ...
In this article, you will learn how to serialize properties of derived classes with the System.Text.Json namespace.
Read more >Property form - Completing the General tab for Page modes
The concrete class for the embedded page cannot differ from the class recorded here. ... Select Java Page to enable the Serialize this...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@mcalthrop you can also use
lean()
function in Mongoose to have directly plain JSON instead of Mongoose documents 😉@SeyZ: ah, thanks for that… it’s simplified my code. Cheers!