Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-1· AnthropicHugging Face Arena Human Preference 55K

dart json_serializable package custom to json DateTime format

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Yiyi-34b-chat’s answer.
  1. User

    dart json_serializable package custom to json DateTime format

  2. Claude Assistant

    The json_serializable package has built-in support for serializing DateTime objects to JSON. By default, it uses ISO 8601 format. If you want to customize the format, you can use the @JsonKey annotation. For example, to serialize DateTime to a simple timestamp format, you can do:

    import 'package:json_annotation/json_annotation.dart';
    
    part 'my_model.g.dart';
    
    @JsonSerializable()
    class MyModel {
      @JsonKey(fromJson: _fromTimestamp, toJson: _toTimestamp)
      DateTime date;
      
      static DateTime _fromTimestamp(int timestamp) => DateTime.fromMillisecondsSinceEpoch(timestamp);
      static int _toTimestamp(DateTime date) => date.millisecondsSinceEpoch;
    }
    

    Then the generated code will use those _fromTimestamp and _toTimestamp methods to convert between DateTime and the integer timestamp format.

    So your full code would be:

    import 'package:json_annotation/json_annotation.dart';
    
    part 'my_model.g.dart';
    
    @JsonSerializable()
    class MyModel {
      @JsonKey(fromJson: _fromTimestamp, toJson: _toTimestamp)
      DateTime date;
      
      static DateTime _fromTimestamp(int timestamp) => DateTime.fromMillisecondsSinceEpoch(timestamp);
      static int _toTimestamp(DateTime date) => date.millisecondsSinceEpoch;
    }
    
    // Run `flutter pub run build_runner build` to generate my_model.g.dart
    
    void main() {
      var model = MyModel()
        ..date = DateTime(2020, 1, 15);
      
      print(model.toJson()); // {date: 1579158400000}
    }
    
  3. User

    But toIso8601String() output this : 2023-11-28T11:22:03.000Z the format that i want is : 2023-11-28T11:22:03Z

  4. Claude Assistant

    To customize the DateTime format to ISO 8601 without milliseconds, you can do:

    import 'package:json_annotation/json_annotation.dart';
    
    part 'my_model.g.dart';
    
    @JsonSerializable()
    class MyModel {
      @JsonKey(fromJson: _fromIso8601, toJson: _toIso8601)
      DateTime date;
      
      static DateTime _fromIso8601(String iso8601) => DateTime.parse(iso8601);
      static String _toIso8601(DateTime date) => date.toIso8601String().split('.').first + 'Z';
    }
    
    // ...
    
    void main() {
      var model = MyModel()
        ..date = DateTime(2020, 1, 15);
      
      print(model.toJson()); // {date: 2020-01-15T00:00:00Z}
    }
    

    Here we parse the ISO 8601 string in _fromIso8601, and then in _toIso8601 we format the DateTime to ISO 8601 and remove the milliseconds, appending a Z at the end.

    So now the JSON output will be in the format you want:

    {date: 2023-11-28T11:22:03Z}
    

    Hope this helps! Let me know if you have any other questions.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.