Comments (3)
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.
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.
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)
- Runtime error on TextEncoder/TextDecoder HOT 4
- Why is the 'message' type field set as optional? HOT 1
- [feat] Option to generate enum with full prefix HOT 11
- Expose `add` method on registry object HOT 2
- Calling FeatureSetDefaults.fromBinary in a non nodejs runtime fails importing @bufbuild/protobuf HOT 2
- Message name collides with imported message name
- Consider providing a convenient accessor for custom options from Desc* types HOT 3
- Non optional timestamp HOT 1
- JsonFormat does not work for proto with optional fields HOT 1
- Register Default Registry HOT 2
- Avoid `instanceof` and remove the "node" export condition
- TypeScript resolves CJS type definitions for ESM code with `NodeNext` module resolution HOT 5
- Comparing messages (Diff) HOT 2
- JSON serialization options HOT 2
- Get T property of FieldInfo HOT 3
- Expose message class's canonical name HOT 4
- Support option retention in @bufbuild/protoplugin HOT 2
- Regression: enum strings are broken in constructor of message types in 1.8.0 HOT 3
- Using message as a value in Map seems to break deserialisation HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from protobuf-es.