2424 */
2525package de .bluecolored .bluemap .core .map .hires ;
2626
27+ import com .fasterxml .jackson .core .JsonFactory ;
28+ import com .fasterxml .jackson .core .JsonGenerator ;
2729import com .flowpowered .math .TrigMath ;
28- import com .google .gson .Gson ;
29- import com .google .gson .GsonBuilder ;
30- import com .google .gson .stream .JsonWriter ;
3130import de .bluecolored .bluemap .core .util .InstancePool ;
3231import de .bluecolored .bluemap .core .util .MergeSort ;
3332import de .bluecolored .bluemap .core .util .math .MatrixM3f ;
4140import java .nio .charset .StandardCharsets ;
4241import java .util .UUID ;
4342
43+ @ SuppressWarnings ("UnusedReturnValue" )
4444public class HiresTileModel {
4545 private static final double GROW_MULTIPLIER = 1.5 ;
4646
@@ -395,18 +395,21 @@ private void setCapacity(int capacity) {
395395 }
396396
397397 public void writeBufferGeometryJson (OutputStream out ) throws IOException {
398- Gson gson = new GsonBuilder (). create ();
399- JsonWriter json = gson . newJsonWriter (new BufferedWriter (new OutputStreamWriter (out , StandardCharsets .UTF_8 ), 81920 ));
398+ JsonFactory factory = new JsonFactory ();
399+ JsonGenerator json = factory . createGenerator (new BufferedWriter (new OutputStreamWriter (out , StandardCharsets .UTF_8 ), 81920 ));
400400
401- json .beginObject (); // main-object
402- json .name ("tileGeometry" ).beginObject (); // tile-geometry-object
401+ json .writeStartObject (); // main-object
402+ json .writeFieldName ("tileGeometry" );
403+ json .writeStartObject (); // tile-geometry-object
403404
404405 // set special values
405- json .name ("type" ). value ( "BufferGeometry" );
406- json .name ("uuid" ). value ( UUID .randomUUID ().toString ().toUpperCase ());
406+ json .writeStringField ("type" , "BufferGeometry" );
407+ json .writeStringField ("uuid" , UUID .randomUUID ().toString ().toUpperCase ());
407408
408- json .name ("data" ).beginObject (); // data
409- json .name ("attributes" ).beginObject (); // attributes
409+ json .writeFieldName ("data" );
410+ json .writeStartObject (); // data
411+ json .writeFieldName ("attributes" );
412+ json .writeStartObject (); // attributes
410413
411414 writePositionArray (json );
412415 writeNormalArray (json );
@@ -416,47 +419,49 @@ public void writeBufferGeometryJson(OutputStream out) throws IOException {
416419 writeBlocklightArray (json );
417420 writeSunlightArray (json );
418421
419- json .endObject (); // attributes
422+ json .writeEndObject (); // attributes
420423
421424 writeMaterialGroups (json );
422425
423- json .endObject (); // data
424- json .endObject (); // tile-geometry-object
425- json .endObject (); // main-object
426+ json .writeEndObject (); // data
427+ json .writeEndObject (); // tile-geometry-object
428+ json .writeEndObject (); // main-object
426429
427430 // save and return
428431 json .flush ();
432+ json .close ();
429433 }
430434
431- private void writePositionArray (JsonWriter json ) throws IOException {
432- json .name ("position" );
433- json .beginObject ();
435+ private void writePositionArray (JsonGenerator json ) throws IOException {
436+ json .writeFieldName ("position" );
437+ json .writeStartObject ();
434438
435- json .name ("type" ). value ( "Float32Array" );
436- json .name ("itemSize" ). value ( 3 );
437- json .name ("normalized" ). value ( false );
439+ json .writeStringField ("type" , "Float32Array" );
440+ json .writeNumberField ("itemSize" , 3 );
441+ json .writeBooleanField ("normalized" , false );
438442
439- json .name ("array" ).beginArray ();
443+ json .writeFieldName ("array" );
444+ json .writeStartArray ();
440445 int posSize = size * FI_POSITION ;
441446 for (int i = 0 ; i < posSize ; i ++) {
442447 writeRounded (json , position [i ]);
443448 }
444- json .endArray ();
445- json .endObject ();
449+ json .writeEndArray ();
450+ json .writeEndObject ();
446451 }
447452
448- private void writeNormalArray (JsonWriter json ) throws IOException {
453+ private void writeNormalArray (JsonGenerator json ) throws IOException {
449454 VectorM3f normal = new VectorM3f (0 , 0 , 0 );
450455
451- json .name ("normal" );
452- json .beginObject ();
456+ json .writeFieldName ("normal" );
457+ json .writeStartObject ();
453458
454- json .name ("type" ).value ("Float32Array" );
455- json .name ("itemSize" ).value (3 );
456- json .name ("normalized" ).value (false );
457-
458- json .name ("array" ).beginArray ();
459+ json .writeStringField ("type" , "Float32Array" );
460+ json .writeNumberField ("itemSize" , 3 );
461+ json .writeBooleanField ("normalized" , false );
459462
463+ json .writeFieldName ("array" );
464+ json .writeStartArray ();
460465 int pi , i , j ;
461466 for (i = 0 ; i < size ; i ++) {
462467 pi = i * FI_POSITION ;
@@ -473,19 +478,20 @@ private void writeNormalArray(JsonWriter json) throws IOException {
473478 writeRounded (json , normal .z );
474479 }
475480 }
476- json .endArray ();
477- json .endObject ();
481+ json .writeEndArray ();
482+ json .writeEndObject ();
478483 }
479484
480- private void writeColorArray (JsonWriter json ) throws IOException {
481- json .name ("color" );
482- json .beginObject ();
485+ private void writeColorArray (JsonGenerator json ) throws IOException {
486+ json .writeFieldName ("color" );
487+ json .writeStartObject ();
483488
484- json .name ("type" ). value ( "Float32Array" );
485- json .name ("itemSize" ). value ( 3 );
486- json .name ("normalized" ). value ( false );
489+ json .writeStringField ("type" , "Float32Array" );
490+ json .writeNumberField ("itemSize" , 3 );
491+ json .writeBooleanField ("normalized" , false );
487492
488- json .name ("array" ).beginArray ();
493+ json .writeFieldName ("array" );
494+ json .writeStartArray ();
489495 int colorSize = size * FI_COLOR , i , j ;
490496 for (i = 0 ; i < colorSize ; i += 3 ) {
491497 for (j = 0 ; j < 3 ; j ++) {
@@ -494,123 +500,128 @@ private void writeColorArray(JsonWriter json) throws IOException {
494500 writeRounded (json , color [i + 2 ]);
495501 }
496502 }
497- json .endArray ();
498- json .endObject ();
503+ json .writeEndArray ();
504+ json .writeEndObject ();
499505 }
500506
501- private void writeUvArray (JsonWriter json ) throws IOException {
502- json .name ("uv" );
503- json .beginObject ();
507+ private void writeUvArray (JsonGenerator json ) throws IOException {
508+ json .writeFieldName ("uv" );
509+ json .writeStartObject ();
504510
505- json .name ("type" ). value ( "Float32Array" );
506- json .name ("itemSize" ). value ( 2 );
507- json .name ("normalized" ). value ( false );
511+ json .writeStringField ("type" , "Float32Array" );
512+ json .writeNumberField ("itemSize" , 2 );
513+ json .writeBooleanField ("normalized" , false );
508514
509- json .name ("array" ).beginArray ();
515+ json .writeFieldName ("array" );
516+ json .writeStartArray ();
510517 int uvSize = size * FI_UV ;
511518 for (int i = 0 ; i < uvSize ; i ++) {
512519 writeRounded (json , uv [i ]);
513520 }
514- json .endArray ();
515- json .endObject ();
521+ json .writeEndArray ();
522+ json .writeEndObject ();
516523 }
517524
518- private void writeAoArray (JsonWriter json ) throws IOException {
519- json .name ("ao" );
520- json .beginObject ();
525+ private void writeAoArray (JsonGenerator json ) throws IOException {
526+ json .writeFieldName ("ao" );
527+ json .writeStartObject ();
521528
522- json .name ("type" ). value ( "Float32Array" );
523- json .name ("itemSize" ). value ( 1 );
524- json .name ("normalized" ). value ( false );
529+ json .writeStringField ("type" , "Float32Array" );
530+ json .writeNumberField ("itemSize" , 1 );
531+ json .writeBooleanField ("normalized" , false );
525532
526- json .name ("array" ).beginArray ();
533+ json .writeFieldName ("array" );
534+ json .writeStartArray ();
527535 int aoSize = size * FI_AO ;
528536 for (int i = 0 ; i < aoSize ; i ++) {
529537 writeRounded (json , ao [i ]);
530538 }
531- json .endArray ();
532- json .endObject ();
539+ json .writeEndArray ();
540+ json .writeEndObject ();
533541 }
534542
535- private void writeBlocklightArray (JsonWriter json ) throws IOException {
536- json .name ("blocklight" );
537- json .beginObject ();
543+ private void writeBlocklightArray (JsonGenerator json ) throws IOException {
544+ json .writeFieldName ("blocklight" );
545+ json .writeStartObject ();
538546
539- json .name ("type" ). value ( "Float32Array" );
540- json .name ("itemSize" ). value ( 1 );
541- json .name ("normalized" ). value ( false );
547+ json .writeStringField ("type" , "Float32Array" );
548+ json .writeNumberField ("itemSize" , 1 );
549+ json .writeBooleanField ("normalized" , false );
542550
543- json .name ("array" ).beginArray ();
551+ json .writeFieldName ("array" );
552+ json .writeStartArray ();
544553 int blSize = size * FI_BLOCKLIGHT ;
545554 for (int i = 0 ; i < blSize ; i ++) {
546- json .value (blocklight [i ]);
547- json .value (blocklight [i ]);
548- json .value (blocklight [i ]);
555+ json .writeNumber (blocklight [i ]);
556+ json .writeNumber (blocklight [i ]);
557+ json .writeNumber (blocklight [i ]);
549558 }
550- json .endArray ();
551- json .endObject ();
559+ json .writeEndArray ();
560+ json .writeEndObject ();
552561 }
553562
554- private void writeSunlightArray (JsonWriter json ) throws IOException {
555- json .name ("sunlight" );
556- json .beginObject ();
563+ private void writeSunlightArray (JsonGenerator json ) throws IOException {
564+ json .writeFieldName ("sunlight" );
565+ json .writeStartObject ();
557566
558- json .name ("type" ). value ( "Float32Array" );
559- json .name ("itemSize" ). value ( 1 );
560- json .name ("normalized" ). value ( false );
567+ json .writeStringField ("type" , "Float32Array" );
568+ json .writeNumberField ("itemSize" , 1 );
569+ json .writeBooleanField ("normalized" , false );
561570
562- json .name ("array" ).beginArray ();
571+ json .writeFieldName ("array" );
572+ json .writeStartArray ();
563573 int blSize = size * FI_SUNLIGHT ;
564574 for (int i = 0 ; i < blSize ; i ++) {
565- json .value (sunlight [i ]);
566- json .value (sunlight [i ]);
567- json .value (sunlight [i ]);
575+ json .writeNumber (sunlight [i ]);
576+ json .writeNumber (sunlight [i ]);
577+ json .writeNumber (sunlight [i ]);
568578 }
569- json .endArray ();
570- json .endObject ();
579+ json .writeEndArray ();
580+ json .writeEndObject ();
571581 }
572582
573- private void writeMaterialGroups (JsonWriter json ) throws IOException {
574- json .name ("groups" ).beginArray (); // groups
583+ private void writeMaterialGroups (JsonGenerator json ) throws IOException {
584+ json .writeFieldName ("groups" );
585+ json .writeStartArray (); // groups
575586
576587 if (size > 0 ) {
577-
578588 int miSize = size * FI_MATERIAL_INDEX , lastMaterial = materialIndex [0 ], material = lastMaterial , groupStart = 0 ;
579589
580- json .beginObject ();
581- json .name ("materialIndex" ). value ( material );
582- json .name ("start" ). value ( 0 );
590+ json .writeStartObject ();
591+ json .writeNumberField ("materialIndex" , material );
592+ json .writeNumberField ("start" , 0 );
583593
584594 for (int i = 1 ; i < miSize ; i ++) {
585595 material = materialIndex [i ];
586596
587597 if (material != lastMaterial ) {
588- json .name ("count" ). value ( (i - groupStart ) * 3L );
589- json .endObject ();
598+ json .writeNumberField ("count" , (i - groupStart ) * 3L );
599+ json .writeEndObject ();
590600
591601 groupStart = i ;
592602
593- json .beginObject ();
594- json .name ("materialIndex" ). value ( material );
595- json .name ("start" ). value ( groupStart * 3L );
603+ json .writeStartObject ();
604+ json .writeNumberField ("materialIndex" , material );
605+ json .writeNumberField ("start" , groupStart * 3L );
596606 }
597607
598608 lastMaterial = material ;
599609 }
600610
601- json .name ("count" ).value ((miSize - groupStart ) * 3L );
602- json .endObject ();
603-
611+ json .writeNumberField ("count" , (miSize - groupStart ) * 3L );
612+ json .writeEndObject ();
604613 }
605614
606- json .endArray (); // groups
615+ json .writeEndArray (); // groups
607616 }
608617
609- private void writeRounded (JsonWriter json , double value ) throws IOException {
618+ private void writeRounded (JsonGenerator json , double value ) throws IOException {
610619 // rounding and remove ".0" to save string space
611620 double d = Math .round (value * 10000d ) / 10000d ;
612- if (d == (long ) d ) json .value ((long ) d );
613- else json .value (d );
621+ if (d == (long ) d )
622+ json .writeNumber ((long ) d );
623+ else
624+ json .writeNumber (d );
614625 }
615626
616627 public void sort () {
@@ -726,4 +737,4 @@ public static InstancePool<HiresTileModel> instancePool() {
726737 return INSTANCE_POOL ;
727738 }
728739
729- }
740+ }
0 commit comments