question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

serializer example on @jsonObejct

See original GitHub issue

I used serializer on @jsonObejct, but it has error below,

Argument of type '{ serializer: (myClass: MyClass) => any; }' is not assignable to parameter of type 'ParameterlessConstructor<{}>'.
  Object literal may only specify known properties, and 'serializer' does not exist in type 'ParameterlessConstructor<{}>'.ts(2345)

My codes like

@jsonObject({
  serializer: MyClass.toJson // error spot
})
export class MyClass extends BaseEntity {
  static toJson(myClass: MyClass): any {
    //serialization from MyClass to json.
  }
 :
}

Anyone give me some example using serializer on @jsonObejct? Or any idea?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
Neos3452commented, Jun 4, 2019

You could have something like this. I’ve also added a test for that. So, you can take a look in the repository.

    @jsonObject
    class Inner {
        @jsonMember
        prop: string;

        shouldSerialize: boolean;
    }

    function objArraySerializer(values: Inner[]) {
        return TypedJSON.toPlainArray(
            values.filter(value => value.shouldSerialize),
            Inner,
        );
    }

    @jsonObject
    class Obj {
        @jsonArrayMember(Inner, { serializer: objArraySerializer })
        inners: Inner[];

        @jsonMember
        str: string;
    }
0reactions
pyunghwacommented, Jun 10, 2019

I found it out. Instead of “null”, you can use “undefined”.

@jsonObject
class Obj {
    @jsonMember({ serializer: value => undefined })
    str: string;
}

The result of serializing this code is blank { }.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to serialize JSON object in java - Stack Overflow
Have a look at Gson library. It does nice JSON to object mapping and serialization. ... Use the .toString() method of JSONObject to...
Read more >
How to serialize a JSON object with JsonWriter using Object ...
In the below example, we can serialize a JSON object using the JsonWriter interface. Example. import java.io.StringWriter; import ...
Read more >
JSON serialization and deserialization in Java with JSON-Java
Java JSON tutorial shows how to do JSON serialization and deserialization ... In the following example, we create a JSONObject from a Map....
Read more >
JSON Serialize | Guide to Syntax and Examples of ... - eduCBA
Example #3. Javascript JSON object serialization using a numerical value. The argument is used to control white spaces and control indentation.
Read more >
Serializing and Deserializing JSON - Json.NET
The quickest method of converting between JSON text and a .NET object is using the T:Newtonsoft.Json.JsonSerializer. The JsonSerializer converts .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found