Skip to content

Commit c292153

Browse files
committed
docs: load from_json() #3
1 parent 1c5120d commit c292153

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

README.md

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
[![NPM Downloads](https://img.shields.io/npm/dm/%40lenml%2Fchar-card-reader)](https://www.npmjs.com/package/@lenml/char-card-reader)
44
[![NPM Version](https://img.shields.io/npm/v/%40lenml%2Fchar-card-reader)](https://www.npmjs.com/package/@lenml/char-card-reader)
55

6-
A lightweight library for reading SillyTavern character card metadata from image files (PNG, JPEG, WEBP) without external dependencies.
6+
A lightweight library for reading SillyTavern character card metadata from image files (PNG, JPEG, WEBP) and JSON without external dependencies.
77

88
## Features
99

1010
- Supports character card specifications v1, v2, and v3
11-
- Extracts metadata from PNG, JPEG, and WEBP images
11+
- Extracts metadata from PNG, JPEG, WEBP images and JSON data
1212
- Provides conversion between different spec versions
1313
- Zero external dependencies
1414
- Works in both Node.js and browser environments
@@ -32,9 +32,18 @@ import { CharacterCard } from "@lenml/char-card-reader";
3232
import fs from "fs";
3333

3434
(async () => {
35+
// Load from image file
3536
const file = fs.readFileSync("./path/to/character.png");
3637
const card = await CharacterCard.from_file(file);
3738

39+
// Or load from JSON data
40+
const jsonData = {
41+
name: "Example Character",
42+
description: "A friendly AI assistant",
43+
// ... other character properties
44+
};
45+
const jsonCard = CharacterCard.from_json(jsonData);
46+
3847
// Access card properties
3948
console.log("Character Name:", card.name);
4049
console.log("Description:", card.description);
@@ -59,10 +68,19 @@ import fs from "fs";
5968
const file = e.target.files[0];
6069
if (!file) return;
6170
62-
const arrayBuffer = await file.arrayBuffer();
63-
const card = await CharacterCard.from_file(arrayBuffer);
64-
65-
console.log("Character Info:", card.toSpecV3());
71+
// Load from image file
72+
if (file.type.startsWith("image/")) {
73+
const arrayBuffer = await file.arrayBuffer();
74+
const card = await CharacterCard.from_file(arrayBuffer);
75+
console.log("Character Info:", card.toSpecV3());
76+
}
77+
// Or load from JSON file
78+
else if (file.type === "application/json") {
79+
const jsonText = await file.text();
80+
const jsonData = JSON.parse(jsonText);
81+
const card = CharacterCard.from_json(jsonData);
82+
console.log("Character Info:", card.toSpecV3());
83+
}
6684
});
6785
</script>
6886
```
@@ -74,6 +92,7 @@ import fs from "fs";
7492
#### Static Methods
7593

7694
- `from_file(file: ArrayBuffer | Uint8Array): Promise<CharacterCard>` - Creates a CharacterCard instance from a file
95+
- `from_json(raw_data: CharRawData, fallback_avatar = ""): CharacterCard` - Creates a CharacterCard instance from JSON data
7796

7897
#### Instance Properties
7998

0 commit comments

Comments
 (0)