Unable to Generate toJson Method Using Serverpod

197 views Asked by At

I'm having trouble using the serialization process for serverpod. Here's the repo if you'd like to take a look: https://github.com/fhir-fli/fhirpod. All of my classes are freezed, and I've adjusted all of the fromJson to include the serializationManagers. That has no errors. When I try to generate code, if I remove json_serializable from the pubspec and all of the part 'file.g.dart'; code, then it generates fine. However, it doesn't generate the toJson() files that are required.

However, when I do include the json_serializable to generate the toJson() methods, I get this error message:

[SEVERE] json_serializable on lib/r4/resource_types/financial/general/general.dart:

Expecting a `fromJson` constructor with exactly one positional parameter. The only extra parameters allowed are functions of the form `T Function(Object?) fromJsonT` where `T` is a type parameter of the target type.
package:fhir/r4/special_types/special_types.dart:483:16

I'm using serverpod_serialization: ^1.1.0.

Just hoping someone else had a similar experience and had found a solution.

1

There are 1 answers

0
Grey On

This is only a partial answer (and kind of hacky), so anyone else please feel free to add on. Ensure that you have serverpod_serialization in your pubspec.yaml file and imported into any files where you want to serialize things.

Run flutter pub run build_runner watch --delete-conflicting-outputs. Again, this is only a partial answer because you can't run this after the fix. Then, do a search and replace on all files you want to generate for serverpod. I did something like, search for:

.fromJson(Map<String, dynamic> json

and replace it with:

.fromJson(Map<String, dynamic> json, [SerializationManager? serializationManager,]

At this point, in your server directory, import this package. Then, in the server/config/generate.yaml file, be sure to add any classes under extraClasses:

type: server

client_package_path: ../fhirpod_client

extraClasses:
  - package:fhir/r5.dart:Account
  - package:fhir/r5.dart:ActivityDefinition
  - package:fhir/r5.dart:ActorDefinition

You can now run serverpod generate and it will generate server-side code, but only as a SerializedEntity, not as a TableRow.