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.

Make ListSerializer() a function on an object like serializer()

See original GitHub issue

What is your use-case and why do you need this feature? Hey,

I just migrated to 1.0.0-RC and had to change my list serializations from MyClass.serializer().list to ListSerializer(MyClass.serializer()). I thought ListSerializer is a class and I need to create an instance of it, but it’s actually a method, what’s quite confusing.

I think this new solution is quite hard to find, if you don’t know it exists.

Describe the solution you’d like I’m wondering why it’s not simply MyClass.listSerializer() or just like before MyClass.serializer().list()? You can add similar methods for Sets and Maps. It’s way more straight-forward to understand and is consistent with how you use the “default” serializer.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tom5079commented, Aug 26, 2020

I think the reason behind the change is to follow the philosophy of object-oriented programming.

serializer.list implies that the single object serializer has a list serializer as a member of it, which sounds somewhat weird. ListSerializer(serializer) implies that the list serializer is containing a single object serializer, and it sounds more plausible.

I thought ListSerializer is a class and I need to create an instance of it, but it’s actually a method, what’s quite confusing.

You could think of it as a class constructor, indeed it is.

public fun <T> ListSerializer(elementSerializer: KSerializer<T>): KSerializer<List<T>> =
    ArrayListSerializer(elementSerializer)

calling a class constructor and top-level method is no different in Kotlin so where is your confusion coming from?

0reactions
Syexcommented, Aug 27, 2020

You can just write format.encodeToXxx(data) where data is of List<MyClass> type, and the appropriate serializer will be instantiated automatically

That’s pretty neat. Seems for me then it was only a migration issue.

Thanks for your explanation, very helpful, closing it then 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Serializers - Django REST framework
Serializers allow complex data such as querysets and model ... In some cases the .create() and .update() method names may not be meaningful....
Read more >
django - How to use ListSerializer with a ModelSerializer?
The serializer.data property is only valid if you have a saved an instance to the serializer. Either call serializer.save() or use ...
Read more >
Serializers - Django REST framework - Tom Christie
Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered...
Read more >
Multiple Create: 'ListSerializer' object is not iterable #5502
I am trying to make a ViewSet that allows multiple-creation. It does actually work, however it throws an error in my logs.
Read more >
ListSerializer -- Classy DRF
Detailed descriptions, with full methods and attributes of ListSerializer Django REST Framework class.
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