Marco
Marco

Reputation: 593

Flutter/Dart JSON and serialization of an existing library class

I have a class:

import 'package:google_maps_flutter/google_maps_flutter.dart';

class Place {
  Place({
    this.address,
    this.coordinates,
  });

  final String address;
  final LatLng coordinates;
}

LatLng is a class of google_maps_flutter. How can I make my Place class serializable using json_annotation and json_serializable?

Thank you very much!

Upvotes: 6

Views: 1357

Answers (3)

Volker Andres
Volker Andres

Reputation: 851

I didn't find a super easy solution for this. I ended up, writing a custom toJson/fromJson for the LatLng property. I've put it statically onto my model, but you could also create a global function, if you need to reuse it.

Take care, that in my example LatLng is nullable.

import 'dart:collection';
import 'dart:convert';

import 'package:latlong2/latlong.dart';

part 'challenge_model.g.dart';

@JsonSerializable()
class ChallengeModel with _$ChallengeModel {
  ChallengeModel({
    required this.startPosition,
  });

  @override
  @JsonKey(fromJson: latLngFromJson, toJson: latLngToJson)
  final LatLng? startPosition;

  /// Create [ChallengeModel] from a json representation
  static fromJson(Map<String, dynamic> json) => _$ChallengeModelFromJson(json);

  /// Json representation
  @override
  Map<String, dynamic> toJson() => _$ChallengeModelToJson(this);

  static String latLngToJson(LatLng? latLng) =>
      jsonEncode(latLng != null ? {'latitude': latLng.latitude, 'longitude': latLng.longitude} : null);

  static LatLng? latLngFromJson(String jsonString) {
    final LinkedHashMap<String, dynamic>? jsonMap = jsonDecode(jsonString);

    return jsonMap != null ? LatLng(jsonMap['latitude'], jsonMap['longitude']) : null;
  }
}

Upvotes: 0

grungegurunge
grungegurunge

Reputation: 891

You can explicitly specify which methods of LatLng should be used for serialization using JsonKey annotation:

@JsonSerializable()
class Place {
  Place({
    this.address,
    this.coordinates,
  });

  final String address;
  @JsonKey(fromJson: LatLng.fromJson, toJson: jsonEncode)
  final LatLng coordinates;
}

Upvotes: 2

Amin Al-jebbeh
Amin Al-jebbeh

Reputation: 302

Put this code in your model

to get the information from JSON response simply do that after your request

  1. final place = placeFromJson(response.body);
  2. to get address => = place.address
  3. to get coordinates => place.coordinates.lng , place.coordinates.lat

=============================================


import 'dart:convert';

Place placeFromJson(String str) => Place.fromJson(json.decode(str));

String placeToJson(Place data) => json.encode(data.toJson());

class Place {
    String address;
    Coordinates coordinates;

    Place({
        this.address,
        this.coordinates,
    });

    factory Place.fromJson(Map<String, dynamic> json) => Place(
        address: json["address"],
        coordinates: Coordinates.fromJson(json["coordinates"]),
    );

    Map<String, dynamic> toJson() => {
        "address": address,
        "coordinates": coordinates.toJson(),
    };
}

class Coordinates {
    String lat;
    String lng;

    Coordinates({
        this.lat,
        this.lng,
    });

    factory Coordinates.fromJson(Map<String, dynamic> json) => Coordinates(
        lat: json["lat"],
        lng: json["lng"],
    );

    Map<String, dynamic> toJson() => {
        "lat": lat,
        "lng": lng,
    };
}

Upvotes: 0

Related Questions