@@ -14,6 +14,7 @@ use std::fs;
1414use std:: path:: { Path , PathBuf } ;
1515
1616use anyhow:: { bail, Context , Result } ;
17+ use kernal_api:: json:: { self , Layout , Value } ;
1718
1819// ---------------------------------------------------------------------------
1920// Sketch detection
@@ -242,8 +243,8 @@ pub fn is_fastled_repo(path: &Path) -> bool {
242243 let lib_json = path. join ( "library.json" ) ;
243244 if lib_json. is_file ( ) {
244245 if let Ok ( txt) = fs:: read_to_string ( & lib_json) {
245- if let Ok ( val) = serde_json :: from_str :: < serde_json :: Value > ( & txt) {
246- if val. get ( "name" ) . and_then ( |v| v . as_str ( ) ) == Some ( "FastLED" ) {
246+ if let Ok ( Value :: Object ( val) ) = json :: parse ( txt. as_bytes ( ) ) {
247+ if matches ! ( val. get( "name" ) , Some ( Value :: String ( name ) ) if name == "FastLED" ) {
247248 return true ;
248249 }
249250 }
@@ -321,8 +322,13 @@ pub fn collect_examples(examples_dir: &Path) -> Vec<String> {
321322pub fn read_fastled_json_ref ( directory : & Path ) -> Option < String > {
322323 let fpath = directory. join ( "fastled.json" ) ;
323324 let txt = fs:: read_to_string ( fpath) . ok ( ) ?;
324- let value: serde_json:: Value = serde_json:: from_str ( & txt) . ok ( ) ?;
325- value. get ( "ref" ) ?. as_str ( ) . map ( str:: to_owned)
325+ let Value :: Object ( value) = json:: parse ( txt. as_bytes ( ) ) . ok ( ) ? else {
326+ return None ;
327+ } ;
328+ match value. get ( "ref" ) ? {
329+ Value :: String ( value) => Some ( value. clone ( ) ) ,
330+ _ => None ,
331+ }
326332}
327333
328334/// Derive the resolved FastLED ref name from a cached repo directory.
@@ -344,25 +350,22 @@ pub fn cached_repo_ref_name(repo_root: &Path) -> String {
344350/// keys when the file already contains valid JSON.
345351pub fn write_fastled_json_ref ( directory : & Path , ref_name : & str ) -> Result < ( ) > {
346352 let fpath = directory. join ( "fastled.json" ) ;
347- let mut data = if fpath. is_file ( ) {
348- fs :: read_to_string ( & fpath )
349- . ok ( )
350- . and_then ( |txt| {
351- serde_json :: from_str :: < serde_json :: Map < String , serde_json :: Value > > ( & txt ) . ok ( )
352- } )
353- . unwrap_or_default ( )
354- } else {
355- serde_json :: Map :: new ( )
353+ let mut data = match fs :: read_to_string ( & fpath) . ok ( ) {
354+ Some ( txt ) => match json :: parse ( txt . as_bytes ( ) ) {
355+ Ok ( Value :: Object ( value ) ) => value ,
356+ Ok ( _ ) | Err ( json :: Error :: InvalidSyntax ) => std :: collections :: BTreeMap :: new ( ) ,
357+ // Do not erase valid user settings merely because the bounded
358+ // parser cannot retain their complete contents.
359+ Err ( error ) => return Err ( error ) . context ( "read existing fastled.json" ) ,
360+ } ,
361+ None => std :: collections :: BTreeMap :: new ( ) ,
356362 } ;
357363
358- data. insert (
359- "ref" . to_owned ( ) ,
360- serde_json:: Value :: String ( ref_name. to_owned ( ) ) ,
361- ) ;
364+ data. insert ( "ref" . to_owned ( ) , Value :: String ( ref_name. to_owned ( ) ) ) ;
362365
363- let mut json = serde_json :: to_string_pretty ( & serde_json :: Value :: Object ( data ) )
364- . context ( "serialize fastled.json" ) ?;
365- json. push ( '\n' ) ;
366+ let mut json =
367+ json :: encode ( & Value :: Object ( data ) , Layout :: Pretty ) . context ( "serialize fastled.json" ) ?;
368+ json. push ( b '\n') ;
366369 fs:: write ( & fpath, json) . with_context ( || format ! ( "write {}" , fpath. display( ) ) ) ?;
367370 Ok ( ( ) )
368371}
@@ -424,10 +427,13 @@ fn fetch_latest_release_tag() -> Option<String> {
424427 }
425428
426429 let body = resp. into_bytes ( ) . ok ( ) ?;
427- let json: serde_json:: Value = serde_json:: from_slice ( & body) . ok ( ) ?;
428- json. get ( "tag_name" )
429- . and_then ( |v : & serde_json:: Value | v. as_str ( ) )
430- . map ( |s : & str | s. to_owned ( ) )
430+ let Value :: Object ( json) = json:: parse ( & body) . ok ( ) ? else {
431+ return None ;
432+ } ;
433+ match json. get ( "tag_name" ) ? {
434+ Value :: String ( value) => Some ( value. clone ( ) ) ,
435+ _ => None ,
436+ }
431437}
432438
433439/// Return `true` when `ref_str` looks like a git commit SHA (7–40 hex chars).
@@ -1020,6 +1026,41 @@ mod tests {
10201026 assert_eq ! ( read_fastled_json_ref( dir. path( ) ) . as_deref( ) , Some ( "master" ) ) ;
10211027 }
10221028
1029+ #[ test]
1030+ fn project_json_preserves_nested_unknown_fields_and_layout ( ) {
1031+ let dir = temp_dir ( ) ;
1032+ let path = dir. path ( ) . join ( "fastled.json" ) ;
1033+ fs:: write ( & path, r#"{"extra":[null,true,18446744073709551615,{"日本":"line\ntext"}],"ref":"old","ref":"last"}"# ) . unwrap ( ) ;
1034+ assert_eq ! ( read_fastled_json_ref( dir. path( ) ) . as_deref( ) , Some ( "last" ) ) ;
1035+ write_fastled_json_ref ( dir. path ( ) , "new" ) . unwrap ( ) ;
1036+ assert_eq ! ( fs:: read_to_string( & path) . unwrap( ) , "{\n \" extra\" : [\n null,\n true,\n 18446744073709551615,\n {\n \" 日本\" : \" line\\ ntext\" \n }\n ],\n \" ref\" : \" new\" \n }\n " ) ;
1037+ }
1038+
1039+ #[ test]
1040+ fn project_json_keeps_invalid_and_nonobject_fallback_policy ( ) {
1041+ let dir = temp_dir ( ) ;
1042+ let path = dir. path ( ) . join ( "fastled.json" ) ;
1043+ for input in [ "broken" , "[]" , "null" , "42" , r#"{"ref":false}"# ] {
1044+ fs:: write ( & path, input) . unwrap ( ) ;
1045+ assert_eq ! ( read_fastled_json_ref( dir. path( ) ) , None ) ;
1046+ write_fastled_json_ref ( dir. path ( ) , "master" ) . unwrap ( ) ;
1047+ assert_eq ! (
1048+ fs:: read_to_string( & path) . unwrap( ) ,
1049+ "{\n \" ref\" : \" master\" \n }\n "
1050+ ) ;
1051+ }
1052+ }
1053+
1054+ #[ test]
1055+ fn project_json_resource_limit_does_not_replace_existing_settings ( ) {
1056+ let dir = temp_dir ( ) ;
1057+ let path = dir. path ( ) . join ( "fastled.json" ) ;
1058+ let original = format ! ( "{{\" extra\" :\" {}\" }}" , "x" . repeat( json:: MAX_INPUT_BYTES ) ) ;
1059+ fs:: write ( & path, & original) . unwrap ( ) ;
1060+ assert ! ( write_fastled_json_ref( dir. path( ) , "new" ) . is_err( ) ) ;
1061+ assert_eq ! ( fs:: read_to_string( path) . unwrap( ) , original) ;
1062+ }
1063+
10231064 #[ test]
10241065 fn test_cached_repo_ref_name_strips_fastled_prefix ( ) {
10251066 let path = Path :: new ( "/tmp/fastled-3.9.12" ) ;
@@ -1044,9 +1085,11 @@ mod tests {
10441085 write_fastled_json_ref ( dir. path ( ) , "3.9.12" ) . unwrap ( ) ;
10451086
10461087 let txt = fs:: read_to_string ( dir. path ( ) . join ( "fastled.json" ) ) . unwrap ( ) ;
1047- let value: serde_json:: Value = serde_json:: from_str ( & txt) . unwrap ( ) ;
1048- assert_eq ! ( value. get( "name" ) . and_then( |v| v. as_str( ) ) , Some ( "demo" ) ) ;
1049- assert_eq ! ( value. get( "ref" ) . and_then( |v| v. as_str( ) ) , Some ( "3.9.12" ) ) ;
1088+ let Value :: Object ( value) = json:: parse ( txt. as_bytes ( ) ) . unwrap ( ) else {
1089+ panic ! ( "project settings must be an object" ) ;
1090+ } ;
1091+ assert_eq ! ( value. get( "name" ) , Some ( & Value :: String ( "demo" . into( ) ) ) ) ;
1092+ assert_eq ! ( value. get( "ref" ) , Some ( & Value :: String ( "3.9.12" . into( ) ) ) ) ;
10501093 }
10511094
10521095 #[ test]
0 commit comments