Last active
May 5, 2023 12:08
-
-
Save matarillo/4edadddb5f5f65b5cd44be561d515573 to your computer and use it in GitHub Desktop.
facebookの「個人データをダウンロード」でJSONをエクスポートすると、文字列がISO-8859-1 (Latin-1) 扱いされてて文字化けするので、非ASCII文字のエスケープを解除するときにUTF-8に戻す。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"bytes" | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"os" | |
"strings" | |
) | |
func main() { | |
if len(os.Args) == 1 { | |
src, _ := ioutil.ReadAll(os.Stdin) | |
dest := fixJson(src) | |
fmt.Println(string(dest)) | |
} else { | |
for _, arg := range os.Args[1:] { | |
src, err := readFromFile(arg) | |
if err != nil { | |
continue // skip | |
} | |
dest := fixJson(src) | |
ioutil.WriteFile(arg, dest, os.ModePerm) | |
} | |
} | |
} | |
func readFromFile(fileName string) ([]byte, error) { | |
fRead, err := os.Open(fileName) | |
if err != nil { | |
return nil, err | |
} | |
defer fRead.Close() | |
return ioutil.ReadAll(fRead) | |
} | |
func fixJson(srcData []byte) []byte { | |
var src interface{} | |
if err := json.Unmarshal(srcData, &src); err != nil { | |
fmt.Println("fail Unmarshal") | |
return srcData | |
} | |
dest := convert(src) | |
destData, err := json.Marshal(dest) | |
if err != nil { | |
fmt.Println("fail Marshal") | |
return srcData | |
} | |
return destData | |
} | |
func convert(src interface{}) interface{} { | |
if src == nil { | |
return nil | |
} | |
if mapData, ok := src.(map[string]interface{}); ok { | |
for k, v := range mapData { | |
mapData[k] = convert(v) | |
} | |
return mapData | |
} | |
if arrayData, ok := src.([]interface{}); ok { | |
for i, e := range arrayData { | |
arrayData[i] = convert(e) | |
} | |
return arrayData | |
} | |
if strData, ok := src.(string); ok { | |
return fixString(strData) | |
} | |
return src | |
} | |
func fixString(src string) string { | |
buf := &bytes.Buffer{} | |
reader := strings.NewReader(src) | |
scanner := bufio.NewScanner(reader) | |
scanner.Split(bufio.ScanRunes) | |
for scanner.Scan() { | |
token := scanner.Text() | |
r := []rune(token)[0] | |
b := byte(0xff & r) | |
buf.WriteByte(b) | |
} | |
return string(buf.Bytes()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment