Skip to content

Commit 8a942f1

Browse files
committed
Non templated DocumentSnapshot.fromJSON
1 parent d97009e commit 8a942f1

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

common/api-review/firestore.api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ export class DocumentSnapshot<AppModelType = DocumentData, DbModelType extends D
178178
protected constructor();
179179
data(options?: SnapshotOptions): AppModelType | undefined;
180180
exists(): this is QueryDocumentSnapshot<AppModelType, DbModelType>;
181-
static fromJSON<AppModelType, DbModelType extends DocumentData = DocumentData>(db: Firestore, json: object, converter?: FirestoreDataConverter<AppModelType, DbModelType> | null): DocumentSnapshot<AppModelType, DbModelType>;
181+
static fromJSON(db: Firestore, json: object): DocumentSnapshot;
182+
static fromJSONUsingConverter<AppModelType, DbModelType extends DocumentData = DocumentData>(db: Firestore, json: object, converter: FirestoreDataConverter<AppModelType, DbModelType>): DocumentSnapshot<AppModelType, DbModelType>;
182183
get(fieldPath: string | FieldPath, options?: SnapshotOptions): any;
183184
get id(): string;
184185
readonly metadata: SnapshotMetadata;

docs-devsite/firestore_.documentsnapshot.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ export declare class DocumentSnapshot<AppModelType = DocumentData, DbModelType e
4040
| --- | --- | --- |
4141
| [data(options)](./firestore_.documentsnapshot.md#documentsnapshotdata) | | Retrieves all fields in the document as an <code>Object</code>. Returns <code>undefined</code> if the document doesn't exist.<!-- -->By default, <code>serverTimestamp()</code> values that have not yet been set to their final value will be returned as <code>null</code>. You can override this by passing an options object. |
4242
| [exists()](./firestore_.documentsnapshot.md#documentsnapshotexists) | | Returns whether or not the data exists. True if the document exists. |
43-
| [fromJSON(db, json, converter)](./firestore_.documentsnapshot.md#documentsnapshotfromjson) | <code>static</code> | Builds a <code>DocumentSnapshot</code> instance from a JSON object created by [DocumentSnapshot.toJSON()](./firestore_.documentsnapshot.md#documentsnapshottojson)<!-- -->. |
43+
| [fromJSON(db, json)](./firestore_.documentsnapshot.md#documentsnapshotfromjson) | <code>static</code> | Builds a <code>DocumentSnapshot</code> instance from a JSON object created by [DocumentSnapshot.toJSON()](./firestore_.documentsnapshot.md#documentsnapshottojson)<!-- -->. |
44+
| [fromJSONUsingConverter(db, json, converter)](./firestore_.documentsnapshot.md#documentsnapshotfromjsonusingconverter) | <code>static</code> | Builds a <code>DocumentSnapshot</code> instance from a JSON object created by [DocumentSnapshot.toJSON()](./firestore_.documentsnapshot.md#documentsnapshottojson)<!-- -->. |
4445
| [get(fieldPath, options)](./firestore_.documentsnapshot.md#documentsnapshotget) | | Retrieves the field specified by <code>fieldPath</code>. Returns <code>undefined</code> if the document or field doesn't exist.<!-- -->By default, a <code>serverTimestamp()</code> that has not yet been set to its final value will be returned as <code>null</code>. You can override this by passing an options object. |
4546
| [toJSON()](./firestore_.documentsnapshot.md#documentsnapshottojson) | | Returns a JSON-serializable representation of this <code>DocumentSnapshot</code> instance. |
4647

@@ -128,7 +129,7 @@ Builds a `DocumentSnapshot` instance from a JSON object created by [DocumentSnap
128129
<b>Signature:</b>
129130

130131
```typescript
131-
static fromJSON<AppModelType, DbModelType extends DocumentData = DocumentData>(db: Firestore, json: object, converter?: FirestoreDataConverter<AppModelType, DbModelType> | null): DocumentSnapshot<AppModelType, DbModelType>;
132+
static fromJSON(db: Firestore, json: object): DocumentSnapshot;
132133
```
133134

134135
#### Parameters
@@ -137,7 +138,30 @@ static fromJSON<AppModelType, DbModelType extends DocumentData = DocumentData>(d
137138
| --- | --- | --- |
138139
| db | [Firestore](./firestore_.firestore.md#firestore_class) | |
139140
| json | object | a JSON object represention of a <code>DocumentSnapshot</code> instance. |
140-
| converter | [FirestoreDataConverter](./firestore_.firestoredataconverter.md#firestoredataconverter_interface)<!-- -->&lt;AppModelType, DbModelType&gt; \| null | Converts objects to and from Firestore. |
141+
142+
<b>Returns:</b>
143+
144+
[DocumentSnapshot](./firestore_.documentsnapshot.md#documentsnapshot_class)
145+
146+
an instance of [DocumentSnapshot](./firestore_.documentsnapshot.md#documentsnapshot_class) if the JSON object could be parsed. Throws a [FirestoreError](./firestore_.firestoreerror.md#firestoreerror_class) if an error occurs.
147+
148+
## DocumentSnapshot.fromJSONUsingConverter()
149+
150+
Builds a `DocumentSnapshot` instance from a JSON object created by [DocumentSnapshot.toJSON()](./firestore_.documentsnapshot.md#documentsnapshottojson)<!-- -->.
151+
152+
<b>Signature:</b>
153+
154+
```typescript
155+
static fromJSONUsingConverter<AppModelType, DbModelType extends DocumentData = DocumentData>(db: Firestore, json: object, converter: FirestoreDataConverter<AppModelType, DbModelType>): DocumentSnapshot<AppModelType, DbModelType>;
156+
```
157+
158+
#### Parameters
159+
160+
| Parameter | Type | Description |
161+
| --- | --- | --- |
162+
| db | [Firestore](./firestore_.firestore.md#firestore_class) | |
163+
| json | object | a JSON object represention of a <code>DocumentSnapshot</code> instance. |
164+
| converter | [FirestoreDataConverter](./firestore_.firestoredataconverter.md#firestoredataconverter_interface)<!-- -->&lt;AppModelType, DbModelType&gt; | Converts objects to and from Firestore. |
141165

142166
<b>Returns:</b>
143167

packages/firestore/src/api/snapshot.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,19 @@ export class DocumentSnapshot<
570570
return result;
571571
}
572572

573+
/**
574+
* Builds a `DocumentSnapshot` instance from a JSON object created by
575+
* {@link DocumentSnapshot.toJSON}.
576+
*
577+
* @param firestore - The {@link Firestore} instance the snapshot should be loaded for.
578+
* @param json - a JSON object represention of a `DocumentSnapshot` instance.
579+
* @returns an instance of {@link DocumentSnapshot} if the JSON object could be
580+
* parsed. Throws a {@link FirestoreError} if an error occurs.
581+
*/
582+
static fromJSON(db: Firestore, json: object): DocumentSnapshot {
583+
return DocumentSnapshot.fromJSONInternal(db, json, /* converter = */ null);
584+
}
585+
573586
/**
574587
* Builds a `DocumentSnapshot` instance from a JSON object created by
575588
* {@link DocumentSnapshot.toJSON}.
@@ -580,7 +593,23 @@ export class DocumentSnapshot<
580593
* @returns an instance of {@link DocumentSnapshot} if the JSON object could be
581594
* parsed. Throws a {@link FirestoreError} if an error occurs.
582595
*/
583-
static fromJSON<
596+
static fromJSONUsingConverter<
597+
AppModelType,
598+
DbModelType extends DocumentData = DocumentData
599+
>(
600+
db: Firestore,
601+
json: object,
602+
converter: FirestoreDataConverter<AppModelType, DbModelType>
603+
): DocumentSnapshot<AppModelType, DbModelType> {
604+
return DocumentSnapshot.fromJSONInternal(db, json, converter);
605+
}
606+
607+
/**
608+
* Internal implementation for 'fromJSON' and 'fromJSONUsingCoverter'.
609+
* @internal
610+
* @private
611+
*/
612+
private static fromJSONInternal<
584613
AppModelType,
585614
DbModelType extends DocumentData = DocumentData
586615
>(

0 commit comments

Comments
 (0)