GithubHelp home page GithubHelp logo

Comments (3)

timostamm avatar timostamm commented on June 26, 2024

Hey Matej, the Timestamp message already has a special JSON representation - an ISO 8601 string:

import {Timestamp} from "@bufbuild/protobuf";
const ts = Timestamp.now();
console.log(
  ts.toJsonString() // "2024-02-07T11:59:44.546Z"
);

It's part of the Protobuf standard (documented here), and if you were to change the serialized form, it would no longer be compatible to other Protobuf implementations.

Are you seeing something else than the ISO format? It should parse just fine with most JS libraries if you need to, since it's the same format Date uses when serialized to JSON.

from protobuf-es.

Meemaw avatar Meemaw commented on June 26, 2024

Hey @timostamm.

Yes, we have a bit of a unique use-case. We would like to override the toJson of Timestamp to return plain Date instead. This is because we later serialize the protobuf json object with superjson, which will preserve date objects when consumed on the client.

We were able to workaround by overriding the Timestamp's proto:

Timestamp.prototype.toJson = function toJson() {
  const ms = Number(this.seconds) * 1000
  if (
    ms < Date.parse("0001-01-01T00:00:00Z") ||
    ms > Date.parse("9999-12-31T23:59:59Z")
  ) {
    throw new Error(
      `cannot encode google.protobuf.Timestamp to JSON: must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive`,
    )
  }
  if (this.nanos < 0) {
    throw new Error(
      `cannot encode google.protobuf.Timestamp to JSON: nanos must not be negative`,
    )
  }
  return new Date(ms)
}

This is probably a reasonable approach, and I assume you probably don't want to support something like this in the library.

from protobuf-es.

timostamm avatar timostamm commented on June 26, 2024

Matej, overriding the method like this is an appropriate solution 👍

You could wrap this into a function that overrides the method, serializes to JSON, then restores the method, so that it becomes free of side-effects.

from protobuf-es.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.